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
{
Q_OBJECT
public:
explicit CustomPushButton
(QWidget *parent
= 0);
~CustomPushButton() = default;
};
class CustomPushButton: public QPushButton
{
Q_OBJECT
public:
explicit CustomPushButton(QWidget *parent = 0);
~CustomPushButton() = default;
};
To copy to clipboard, switch view to plain text mode
.cpp
CustomPushButton
::CustomPushButton(QWidget *parent
){
setStyleSheet("background-color: black;"); // this does not work
}
CustomPushButton::CustomPushButton(QWidget *parent)
: QWidget(parent)
{
setStyleSheet("background-color: black;"); // this does not work
}
To copy to clipboard, switch view to plain text mode
Bookmarks