Originally Posted by
smacchia
And while it the code compiles, is logically incorrect.
Yes.
I've been using palettes for this very thing successfully (but I must admit, I've been using it for everything *but* labels). The app I am working on has MANY custom colors for every concievable role/state, so I've had to use the palette extensively.
Labels are tricky. This works for all styles I could test (excluding aqua and windowsxp):
#include <QLabel>
#include <QApplication>
int main(int argc, char **argv){
label.setText("Testing");
label.setStyleSheet("background-color: red;");
label.setAutoFillBackground(true);
label.show();
return app.exec();
}
#include <QLabel>
#include <QApplication>
int main(int argc, char **argv){
QApplication app(argc, argv);
QLabel label;
label.setText("Testing");
label.setStyleSheet("background-color: red;");
label.setAutoFillBackground(true);
label.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
This works as well:
#include <QLabel>
#include <QApplication>
int main(int argc, char **argv){
label.setText("Testing");
pal.setColor(label.backgroundRole(), Qt::red);
label.setPalette(pal);
label.setAutoFillBackground(true);
label.show();
return app.exec();
}
#include <QLabel>
#include <QApplication>
int main(int argc, char **argv){
QApplication app(argc, argv);
QLabel label;
label.setText("Testing");
QPalette pal = label.palette();
pal.setColor(label.backgroundRole(), Qt::red);
label.setPalette(pal);
label.setAutoFillBackground(true);
label.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks