PDA

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



hans.krebs42
4th October 2015, 11: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:

.h


class CustomPushButton: public QPushButton
{
Q_OBJECT

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


.cpp


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

prasad_N
4th October 2015, 18: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)