#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStyle>
#include <QDebug>
class App : public QObject
{
Q_OBJECT
public:
enum Alert
{
None,
Warning,
Critical
};
Q_ENUM(Alert);
App(int argc, char** argv)
: app(argc, argv)
{}
int exec()
{
window.setCentralWidget(&main);
label.setText("hello world");
label.setProperty("alert", QVariant::fromValue(None));
button.setText("alert");
QObject::connect(&button, &QPushButton::clicked, [this]()
{
label.setProperty("alert", QVariant::fromValue(Critical));
label.setProperty("foo", false);
label.style()->unpolish(&label);
label.style()->polish(&label);
label.update();
});
layout.addWidget(&label);
layout.addWidget(&button);
main.setLayout(&layout);
app.setStyleSheet(R"(
QLabel[alert="None"] { color: green; }
QLabel[alert="Warning"] { color: blue; }
QLabel[alert="Critical"][foo=true] { color: red; }
QLabel[alert="Critical"][foo=false] { color: magenta; }
)");
window.show();
return app.exec();
}
QApplication app;
QMainWindow window;
QWidget main;
QLabel label;
QPushButton button;
QVBoxLayout layout;
};
int main(int argc, char** argv)
{
return App(argc, argv).exec();
}