Question

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:

  1. Make a new qt widget project
  2. Make a QWidget object, maybe put a button or two there to make sure you know it's being used.
  3. Change the style sheet to the widget made in step 2 to contain: background-image: url(<your image here>);
  4. In the MainWindow ui file, add the QDockWidget, promote it's main widget to the QWidget 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)?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top