Cannot add a background image to a QDockWidget
-
02-06-2021 - |
문제
As far as I can tell, the way you make the background of a widget to be an image is to modify the qstylesheet to say:
background-image: url(<your image here>);
Which seems to work, unless it's the QDockWidget
's main widget.
This is what I did:
- Make a new qt widget project
- Make a
QWidget
object, maybe put a button or two there to make sure you know it's being used. - Change the style sheet to the widget made in step 2 to contain:
background-image: url(<your image here>);
- In the MainWindow ui file, add the
QDockWidget
, promote it's main widget to theQWidget
made in step #2.
When you run the program, you notice that the QDockWidget
's background is still the default, however any buttons or labels you added to the widget have changed their backgrounds to the image you selected.
When you change the qss line from background-image
to background-color
, and select a color rather than an image, the widget does change color.
Does anyone have any suggestions for giving a QDockWidget
a background image (and not just a background color)?
해결책
Apparently all you need to do is overwrite the widget's pain event with:
void MyWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}