PDA

View Full Version : Using custom Q_PROPERTY with Stylesheet



hubbobubbo
22nd April 2010, 12:05
Hi

I have added a custom Q_PROPERTY like this:

Q_PROPERTY( bool isTalking READ isTalking WRITE setIsTalking )

bool _isTalking;

public:
MyClass(QWidget *parent );
~MyClass();

bool isTalking() const {return _isTalking;}
void setIsTalking(bool newIsTalking){_isTalking = newIsTalking;}

Then from my style sheet file I want to change the background color of a QTreeWidgetItem based on this Q_PROPERTY like this:

QTreeWidget#activeCalls::item:isTalking{
background: green;
}

However this has no effect. Using a normal property on the QTreeWidget like this works though:
QTreeWidget#activeCalls::item:selected{
background: grey;
}

So the Widget is named correctly but I do not think the item is updated based on the value of my cusomt Q_PROPERTY. How can I make sure the Widget is updated when my property changes so the correct stylesheet is applied dynamically.

Lykurg
22nd April 2010, 13:08
QTreeWidget#activeCalls::item:selected{
background: grey;
}
You can not access all properties like that. It's only a bunch of properties you can use that way (called pseudo states). You can try to use something like that:QTreeWidget#activeCalls[isTalking="true"]

SABROG
22nd April 2010, 13:32
You must know, what Style Sheets added to QWidget only once and if you change QMetaProperty value later this do not change style with other rule.

hubbobubbo
22nd April 2010, 14:12
Hi

I tried like this (changed to int to make it even simpler)

QTreeWidget#activeCalls::item[isTalking="0"]{
background: green;
}

QTreeWidget#activeCalls::item[isTalking="1"]{
background: yellow;
}

in the code I simple call setIsTalking(0) or setIsTalking(1)

Still no action at all though.

Lykurg
22nd April 2010, 14:39
SABROG gave you the answer: you have to reaply the style sheet every time you change this property. So your approach doesn't seem to be the best. Without knowing exactely what you are doing, I would recomend you to use a custom item delegate where you can paint different backgrounds depending on the items "property". Or if you add widgets, use the widgets paint methode.

hubbobubbo
22nd April 2010, 14:47
Ok thanks I will give up on that approach.

SABROG
22nd April 2010, 14:52
Still no action at all though.
It's standard behaviour. Trolls don't want change this. I created bugreport about one year ago and he's been rejected.

mrandreas
30th September 2010, 10:48
I used this approach:



Q_PROPERTY( bool isTalking
READ isTalking
WRITE setIsTalking
DESGNABLE true
SCRIPTABLE true)

bool _isTalking;
QFileSystemWatcher* _watch

public:
MyClass(QWidget *parent ) :
QWidget(parent)
{
this->setObjectName("MyClass");
_watch->addPath("xxxx.css");
connect(_watch, SIGNAL(fileChanged(QString),
this,SLOT(loadStyleSheet(QString)));

}
~MyClass();

bool isTalking() const
{return _isTalking;}
void setIsTalking(bool newIsTalking)
{_isTalking = newIsTalking;}

public slots:
void loadStyleSheet(const QString& css)
{
QFile cssfile(css);

if(cssfile.open(QIODevice::ReadOnly
| IODevice::Text))
{
QTextStream in(&cssFile);
QString cssssss(in.readAll());
this->setStyleSheet(csssss);
}

}


In the css:


MyClass{qproperty-isTalking: true;}

However - changing properties from css file should not be used for all purposes (at least >5 pr second). it will be slooow.