Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] CSS on custom widget



  • Hi,

    I have a class that inherits from QPushButton. I want to customize it's look and feel with CSS, but I don't know how to access it from CSS.

    The widget is promoted on Qt Designer from QPushButton to the custom class.

    I tried
    @MyClassName {whatever}@

    But it's not working.

    @ui->myButton->metaObject()->className()@

    returns QPushButton, not MyClassName.

    Is there a simple way to access all instances of that class?
    Using QPushButton[objectName|="prefix"] might work for me, but my custom buttons are not named with a "prefix-", they are named with a suffix (and withouht hyphens). Is there an equivalent selector for suffixes or is it possible to use wildcards? I would like to avoid changing the names to keep consistent with the naming style I've been using until now.

    Cheers



  • I just found that [attribute$=value] is just what I need, but QtDesigner complains about it being invalid CSS... I guess Qt does not support CSS3, does it?


  • Moderators

    your class needs to have the Q_OBJECT macro in order to get the correct class name with the meta object system.

    For the supported selector types see "this":http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html#selector-types.





  • Thanks for your quick replies.

    bq. Have You looked on it..
    http://qt-project.org/doc/qt-4.8/stylesheet-examples.html

    yes, that's the first place I looked. But I didn't find a similar example.

    bq. your class needs to have the Q_OBJECT macro in order to get the correct class name with the meta object system.

    You were right, I was missing the Q_OBJECT macro. Thank you very much!

    For future reference, I needed to run qmake (Menu>Build>Run qmake) after adding the macro and prior to compiling. Otherwise I got an
    @undefined reference to `vtable@

    error.

    Needs compiling to see the changes in the CSS (no update on QDesigner)


Log in to reply