View Full Version : Set CSS in QPushButton's subclass's constructor

4th October 2015, 12:57
I'm creating my custom push button class by subclassing QPushButton. However for some reason setting that class's CSS in its constructor has no effect; I have to do it in for example paintEvent, then everything is fine. I could just have a global .qss file and set it for the entire application, but I want the class to manage its own styles. Why doesn't my approach work?

Here is some code:


class CustomPushButton: public QPushButton

explicit CustomPushButton(QWidget *parent = 0);
~CustomPushButton() = default;


CustomPushButton::CustomPushButton(QWidget *parent)
: QWidget(parent)
setStyleSheet("background-color: black;"); // this does not work

4th October 2015, 19:16
It worked for me when I changed constructor to
//your code did not even get compiled on my machine

CustomPushButton::CustomPushButton(QWidget *parent) : QPushButton(parent)