开发者

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

开发者 https://www.devze.com 2023-03-16 16:35 出处:网络
I\'m trying to do so开发者_如何学Pythonme 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 t

I'm trying to do so开发者_如何学Pythonme 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?


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.

class Header1Label : public QLabel
{
    Q_OBJECT

public:
    using QLabel::QLabel;
};

Stylesheet:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消