Using custom Q_PROPERTY with Stylesheet
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.
Re: Using custom Q_PROPERTY with Stylesheet
Quote:
Originally Posted by
hubbobubbo
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:
Code:
QTreeWidget#activeCalls[isTalking="true"]
Re: Using custom Q_PROPERTY with Stylesheet
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.
Re: Using custom Q_PROPERTY with Stylesheet
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.
Re: Using custom Q_PROPERTY with Stylesheet
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.
Re: Using custom Q_PROPERTY with Stylesheet
Ok thanks I will give up on that approach.
Re: Using custom Q_PROPERTY with Stylesheet
Quote:
Originally Posted by
hubbobubbo
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.
Re: Using custom Q_PROPERTY with Stylesheet
I used this approach:
Code:
Q_PROPERTY( bool isTalking
READ isTalking
WRITE setIsTalking
DESGNABLE true
SCRIPTABLE true)
bool _isTalking;
public:
{
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)
{
| IODevice::Text))
{
this->setStyleSheet(csssss);
}
}
In the css:
Code:
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.