Is it possible to set a background image to a widget?
-
wrote on 21 Oct 2010, 14:03 last edited by
How can we place a background image to a QWidget? Can it be set using styles such that all widgets have the same background.
-
wrote on 21 Oct 2010, 16:01 last edited by
The easiest way is to write small stylesheet and apply it to application (or only to needed widgets, if it should not be applied for all widgets). Also you can use style technology and draw it manually.
-
wrote on 21 Oct 2010, 16:07 last edited by
I had tried it but the background image was not getting displayed. Can you provide the syntax for adding a background image to a QWidget.
-
wrote on 21 Oct 2010, 16:28 last edited by
setStyleSheet("background-image: ./image.png");
-
wrote on 21 Oct 2010, 16:46 last edited by
I thik this one will fit better
@
qApp->setStyleSheet("QWidget {background-image: url(./image.png) }");
@ -
wrote on 22 Oct 2010, 06:13 last edited by
and if you just want the background on a particular widget use:
@myWidget->setStyleSheet("background-image: url(./image.png)")@ -
wrote on 22 Oct 2010, 09:57 last edited by
Thank you, I had tried the same before also and then it was not working - now its working. May be I had not built it properly or did some silly mistake then.
-
wrote on 25 Oct 2010, 10:34 last edited by
I am applying the below style using Qt Designer on the root widget
QWidget {background-image: url(./image.png) }
Now in this case all the child widgets also have the same image as background image. How to modify the above to have it only configured as a background image to the root widget.
-
wrote on 25 Oct 2010, 11:34 last edited by
Give name to your widget by setObjectname().
eg:
@
QPushButton *okButton = new QPushButton;
okButton->setObjectname(QObject::tr(okButton));
@
in stylesheet,
@
QPushButton#okButton
{
color: gray
}
@
now only okButton will be gray.Same thing is applicable in your case. Hope it helps!
[edit: Code highlighting / Denis Kormalev]
-
wrote on 25 Oct 2010, 11:37 last edited by
Thanks Rahul
-
wrote on 25 Oct 2010, 12:17 last edited by
Please also check out the docs for style sheets at:
"http://doc.qt.nokia.com/4.7/stylesheet.html":http://doc.qt.nokia.com/4.7/stylesheet.html -
wrote on 5 Nov 2014, 13:37 last edited by
MainWindow w; w.setStyleSheet("background-image:url(./file/left.png)"); w.show();
I did setStyleSheet on MainWindow, it doesn't seem to work
-
wrote on 5 Nov 2014, 13:42 last edited by
MainWindow w; w.setStyleSheet(“background-image:url(:/file/left.png)”); w.show();
WITH ::::::: and if you have a ui file. put setStyleSheet after ui->setupUI(this); -
wrote on 5 Nov 2014, 14:06 last edited by
i have left.png and right.png in my resource folder under /file
which is the correct command? -
wrote on 5 Nov 2014, 14:33 last edited by
@
Error:Could not parse stylesheet of object 0xbfdb6220
ui->setupUi(this);
setStyleSheet("background-image:url(:/left.png;"
"background-repeat:no-repeat;");
@[edit: added missing coding tags @ SGaist]
-
Your style sheet code is wrong, you are missing a closing parenthesis.
-
wrote on 6 Nov 2014, 02:35 last edited by
I think the link below can solve the problem but i can't access my jpg in resources file.
palette->setBrush(QPalette::Background,(new QBrush((new QPixmap(":/1.jpg")))));