Important: Please read the Qt Code of Conduct -

Styling widget in the custom namespace

  • Hello,

    I have a class which inherits QWidget

    namespace nmspc {
        class MyWidget : public QWidget {}

    I wanted to use it in the Qt Designer so I used promotion mechanism, however I needed to specify name of the object (objectName property) to nmspc::MyWidget to have it working.

    Now I am struggling with applying any styles which would target only this widget. Seems like scope resolution operator is messing with css pseudoclasses and without it UI cannot be parsed to valid c++ files.

    Eg. this has not effect, although Qt Designer says its a valid stylesheet

    QWidget#nmspc::MyWidget {
        background-color: red;

    Is there any way to apply a style to a widget named like this? Or maybe the promotion can be done diffrently?

  • :: means subcontrols in stylesheet selectors. So although it is valid, it does not mean what you want to mean.
    There's stylesheet doc for this problem : Widgets Inside C++ Namespaces
    But seems not applicable for id(object name) selector.
    I would suggest using type selector instead of id selector in your case, just like the example in the doc.

    nmspc--MyWidget {
        background-color: red;

    Note: it may have no effect in the designer, but will have effect when you run the app.

  • Wow, I missed that one in the documentation. Although it is not exactly what I had in mind I think I can use it. Thanks!

Log in to reply