Is it possible to use Qt Style Sheets with promoted widgets in Qt Creator?

穿精又带淫゛_ 提交于 2020-01-03 11:48:14

问题


I'm trying to do some heavy redeisgning of standard widgets using Qt Style Sheets. So after doing most of it manually for different widgets by #objectName selectors I've decided to group similar widgets in some way.

For example, I have multiple QFrames which act like headers in inner forms. And I want all of them to have the same stylesheet. One way of doing that is using naming convention (this is my fallback variant), i.e. QFrame[objectName|="prefix_"]. But I wanted to group my widgets by class. So I created simple placeholder class:

class HeaderFrame: public QFrame
{
public:
    HeaderFrame(QWidget *parent = NULL): QFrame(parent) {}
};

Which allowed me to promote all these QFrames to HeaderFrames. After that I've tried setting

HeaderFrame { background-color: red; }

stylesheet to MainWindow object itself (to make it act on all HeaderFrames) but it won't work. Nothing is changed in QtCreator form designer and nothing is changed after compiling an application. I've tried different variants of this stylesheet but nothing works.

So, is only Qt widgets (like QLabel, QFrame, etc.) are available for styling this way? Or there is some way to write stylesheet for your promoted widget?


回答1:


yes,it is possible. The only thing you should keep in mind - base for your derived widgets should support style sheets, and reimplement their PaintEvent carefully.

UPD: example class:

class Header1Label : public QLabel
{
    Q_OBJECT
    public:
    Header1Label(QWidget *parent = 0):QLabel(parent){};
    ~Header1Label(){};
};

style sheet:

Header1Label
{
    font-size:14px;
    font-weight:900;    
}



回答2:


You can always add class-name to object's class property, which is QStringList, and use .class-name selector.



来源:https://stackoverflow.com/questions/6533091/is-it-possible-to-use-qt-style-sheets-with-promoted-widgets-in-qt-creator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!