Get variable name of Qt Widget (for use in Stylesheet)?

后端 未结 1 357
猫巷女王i
猫巷女王i 2021-01-19 02:44

In my application, a User clicks on any widget of my program (which are at the time; dormant) and picks a color for it.
This color will then be added to a stylesheet for

相关标签:
1条回答
  • 2021-01-19 03:49

    You need to first setObjectName("somename") before an object is named, then objectName() will work, or even better - findChild(), or findChildren()

    Example

    header:

    QButton foo;
    

    class:

    foo = new QButton();
    foo.setObjectName("MySuperButton");
    

    Then, finally in your QSS..

    #MySuperButton { 
         background: black;
    }
    

    This also works similarly to CSS with

    QButton#MySuperButton {
         background: red;
    }
    

    The logic behind why you'd want to set multiple object names similarly (for different objects), or use the granularity of only one type of widget with a specific name is also pretty much the same as CSS.

    0 讨论(0)
提交回复
热议问题