PDA

View Full Version : How to properly set a background image of a custom widget



franco.amato
24th January 2021, 22:23
Hi forum,
I have a custom widget, showing the output of a video camera ( after some calculations ) and I would display a background png image.
I tried with the
myWidget->setStyleSheet("background-image: url(:/screen.png);"); with no results.
Should i set it manually in the paint event?
Regards

d_stranz
25th January 2021, 01:31
Should i set it manually in the paint event?

Is the path to the resource correct? (You can test by trying to load in code: QImage::load()).

You can try drawing it in the paint event (using QPainter::drawImage()). Don't load the image in the paintEvent(), load it in your widget constructor and store it as a member variable.

Of course, if your video frame is the same size as the window, setting the background image won't have any effect because the video frame will overwrite it.

franco.amato
25th January 2021, 01:49
Hi,
yes the path is correct.
I would like to display the bg image only while I am not receiving frames from the camera.
Thanx

d_stranz
25th January 2021, 17:29
I would like to display the bg image only while I am not receiving frames from the camera.

The I think the only way to do that is to paint it manually in the paintEvent(). The stylesheet doesn't know anything about your application's details, so your paint event has to decide whether to show the background image or the current frame.