5
votes

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?

2

2 Answers

3
votes

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;    
}
0
votes

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