I want to change the background color in a custom subclass of QWidget. Here is the code:
WorldView::WorldView(QWidget *parent) : QWidget(parent)
{
QPalette p(palette());
p.setColor(QPalette::Background, Qt::black);
setAutoFillBackground(true);
setPalette(p);
}
But it doesn't work as expected. The background color remains unchanged.
I don't know why.
As you can read in the documentation, QPalette::Background
is obsolete. Use QPalette::Window
instead. Note that some widgets use some other role for the background. See the QPalette::ColorRole documentation
Also:
Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the OS X styles.
In this case I suggest to use style sheets. See Qt Style Sheets Reference
However, if WorldView is a custom widget, with your custom paintEvent
, it's up to you to draw the background
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With