Is it possible to set a background image to a widget?
-
How can we place a background image to a QWidget? Can it be set using styles such that all widgets have the same background.
-
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.
-
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.
-
I thik this one will fit better
@
qApp->setStyleSheet("QWidget {background-image: url(./image.png) }");
@ -
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.
-
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.
-
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]
-
Thanks Rahul
-
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 -
Your style sheet code is wrong, you are missing a closing parenthesis.
-
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")))));