[PyQt] Custom widgets, dynamic properties and css

Mark Summerfield mark at qtrac.eu
Tue Nov 20 13:47:35 GMT 2007


On 2007-11-20, Richard Smith wrote:
> Hi Guys,
>
> I've created a custom widget for an application I'm building.
>
> The widget is a simple frame with 2 labels inside it.
>
> Basically, I need the frame style to change dependant on a property of
> the custom widget. I've set the property using .setProperty() method,
> and the initial style of the widget is fine.
>
> For each state of the property, the widget has a style.
>
> However, as the application progresses through it's run, the custom
> property changes as to the state, however the style doesn't change.
>
> I have a couple of methods that are used to change the state and I am
> calling update() after the change, but the style still doesn't change.

I think for styles you must call setStyle() rather than update() to
apply a new (or modified) style.

>
> The important bits of the widget are:
>
> class QuickStatus(QtGui.QFrame):
>         def __init__(self, parent=None):
>                 QtGui.QWidget.__init__(self, parent)
> 		## { ... snip ... }
>
>         def getState(self):
>                 return int(self.property("state").toString())
>
>         def setState(self, state):
>                 if state in STATES:
>                         self.setProperty( "state",
>                                           QtCore.QVariant(state))
>                         self.update()
>
> Then for each of the various states I've got the following styles:
>
> QuickStatus[state="1"]
> {
>  background-color: #CCFF99;
>  border: 1px solid green;
> }
>
> Anyone have any hints as to why the style of the control isn't being
> updated as the state changes? Am I missing something crucial?



-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu



More information about the PyQt mailing list