[SOLVED] Custom Widget with custom stylesheet



  • Hi,

    I used the following example to customise my widget through a style sheet: http://qt-project.org/wiki/Qt_Style_Sheets_and_Custom_Painting_Example

    What is the best way to access the correct colour for the current state. For example I have

    @
    MyWidget {
    qproperty-objectColor: yellow;
    }

    MyWidget::disabled {
    qproperty-objectColor: green;
    }
    @

    and my paint method should get the colour depending on the state.

    Thanks,
    Markus


  • Lifetime Qt Champion

    Hi,

    If i'm not wrong, the property will be updated when your widget stat changes so you simply have to use the objectColor property to retrieve the color in your paintEvent.



  • Unfortunatley not. It stays the same. Here I also have the color property which is from QWidget directly and this one changes (of course?).
    Is it possible that I have to use QStyle somehow? Just using the property does not do the trick. If so the question is how. ;)


  • Moderators

    what type is "MyWidget"?
    I also think the declaration should only contain 1 colon:
    @
    MyWidget:disabled {
    qproperty-objectColor: green;
    }
    @

    Just a hint for the future:

    In your case Qt ensures the polishing, but if you use custom properties in your CSS selectors you have to do like this:

    C++:
    @
    myWidget->setProperty("myProperty", true);
    myWidget->style()->polish(myWidget);
    @

    CSS:
    @
    MyWidget[myProperty="true"] {
    ....
    }
    @



  • MyWidget is derived from QWidget.

    @
    MyWidget:disabled {
    qproperty-objectColor: green;
    }
    @
    does also not work.

    @
    MyWidget {
    qproperty-objectColor: yellow;
    }

    MyWidget[enabled="false"] {
    qproperty-objectColor: green;
    }
    @

    Is doing now exactly what I want it to do with style()->polish(this) in my paintEvent of the widget.

    Thanks!


  • Moderators

    yea stylesheets are a mystery some/most times. ;)

    But don't call style()->polish(this) in the paintEvent since it's not necessary to do the hard work in every paintEvent call. Only if the property changes. Maybe you can even remove it completely and it will still work?



  • Unfortunately it does not work without the polish. The only thing I can do is to check if anything has changed and call polished then if so.


  • Moderators

    better do the polishing on QEvent::EnabledChange.

    Edit:
    another thing which just came to my mind to check: Did you provide the Q_OBJECT macro in your custom widgets class declaration?!



  • Yes, the Q_OBJECT macro is there. I also use a bunch of signals and slots so it has to.

    How do I get this event?


  • Moderators

    in QWidget::event()



  • Works perfect now.
    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.