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
Qt Code:
  1. class CustomPushButton: public QPushButton
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. explicit CustomPushButton(QWidget *parent = 0);
  7. ~CustomPushButton() = default;
  8. };
To copy to clipboard, switch view to plain text mode 

.cpp
Qt Code:
  1. CustomPushButton::CustomPushButton(QWidget *parent)
  2. : QWidget(parent)
  3. {
  4. setStyleSheet("background-color: black;"); // this does not work
  5. }
To copy to clipboard, switch view to plain text mode