PDA

View Full Version : set an image behind all child widgets



quantumpilgim
4th September 2016, 00:19
Hi all,

I am trying to put a background image to a qmainwindow (or better a widget inheriting from qmainwindow). What I know is

window->setStyleSheet("backgour-image:url(myimage.jpg);");

but this somehow tries to "paint" the image on all the widgets which are children to window.

What I WANT to do is is having each widget paint its own space and then the parent window paint any space not taken up by a child.
To make it more precise. Assume that in the constructor of window we have the following:

QPushButton * button1 = new QPushButton("Button 1", this);
QPushButton * button2 = new QPushButton("Button 2", this);
QVBoxLayout * layout = new QVBoxLayout(this);

layout->addWidget(button1);
layout->addWidget(button2);

this->setLayout(this->layout);
/* code to add background image */

If i write this->setStyleSheet("background-image:url(myimage.jpg);"), it will try to put my image on any child of this. What i want is having button1 and button2 having their classical view and in the space between them that layout gives them, myimage is painted (of course in any other place that window might get.

I am pretty sure my question is very simple but i hope that someone takes some time to write me

Thank you very much

anda_skoa
4th September 2016, 09:46
A QMainWindow has only one widget as its content, the "centralWidget", which then contains everything else.

So you could e.g. use a QLabel as the type of the centralWidget and set the image as its pixmap.

Cheers,
_

quantumpilgim
4th September 2016, 12:17
thanks,

yeah i think i tried sth similar in other occasions and it worked.

But I found a perhaps better solution using stylesheets by naming the mainwidget with setQObjectName and then giving the #name as directive in QStyleSheets. continuing on the previous example

this->setObjectName("self");
this->setStyleSheet("#self { border-image:url(myimage.jpg);");

(if i m perfectly honest i didnt try it in Qt but with pyqt5, but i think it should work with C++ as well)

But again thanks