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.
-
setStyleSheet("background-image: ./image.png");
-
I thik this one will fit better
@
qApp->setStyleSheet("QWidget {background-image: url(./image.png) }");
@ -
and if you just want the background on a particular widget use:
@myWidget->setStyleSheet("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 -
MainWindow w; w.setStyleSheet("background-image:url(./file/left.png)"); w.show();
I did setStyleSheet on MainWindow, it doesn't seem to work
-
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); -
i have left.png and right.png in my resource folder under /file
which is the correct command? -
@
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.
-
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")))));