Solved set background image does not work
-
hi folks.
so i have image.png in my source file. and the following code does not work ...what did i do wrong?
this->setStyleSheet("QWidget{background-image: url(:/image.png);}");
-
@qtpi
have you triedthis->setStyleSheet("background-image: url(:/image.png);");
http://stackoverflow.com/questions/4458201/unable-to-set-the-background-image-in-qt-stylesheet
http://stackoverflow.com/questions/22109342/background-image-in-qt-stylesheet-doesnt-work -
@qtpi
this->setStyleSheet("QWidget{background-image: url(:/image.png);}");
Works fine.
For Test I did this, and worked from me.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->setStyleSheet("QWidget{background-image: url(:images/image1.png);}");; }
Check the path , Size and exact name of the image. In my case , I have "image1.png" in the "images" folder. But if you are using QWidget mean you are setting it to all widgets under "this".
And Importantly, after adding images to the resource file, please don't forget to run again the "Run qmake" , build and run.
-
@qtpi
If the path is correct then the question is, what type of widget are you setting the stylesheet on? -
@raven-worx
simply a normal sub-classed qwidget. I subclassed the qwidget twice.
seems still not working.but when I tried to simply run this code on a empty qwidget, it seems to work. I suppose something is wrong with my sub-classed widget.
-
@qtpi
Qt docs say about QWidget:Supports only the background, background-clip and background-origin properties. If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below: void CustomWidget::paintEvent(QPaintEvent *) { QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } The above code is a no-operation if there is no stylesheet set. Warning: Make sure you define the Q_OBJECT macro for your custom widget.
-
@raven-worx O.o I read the code, still did not understand why it works. But it did work.
Thanks for the help! -
@qtpi said:
I read the code, still did not understand why it works.
why? The style draws the widget's "basics".
For other widgets this isn't needed, because they are more complex and thus are drawn via the style anyway.