It is necessary to use some image as the background of the widget (it is listed in the program's resources). How to solve this problem? Tried to do the following:

class WidgetLoading: public QWidget { private: QBrush *brush; QPalette *palette; public: WidgetLoading(); ~WidgetLoading(); }; WidgetLoading::WidgetLoading(): QWidget() { brush = new QBrush; palette = new QPalette; brush->setTextureImage(QImage(":/background.jpg")); palette->setBrush(QPalette::Window, *brush); this->setPalette(*palette); } WidgetLoading::~WidgetLoading() { delete brush; delete palette; } 

But nothing appears. Just a window with a white background. Tell me, please, how can this be done?

    4 answers 4

    Check how the paths to the image are specified in the resource file.

    • The resource file source.qrc is set to the prefix /. It contains the file background.jpg. I appeal to him as described above. What is the mistake here? - carapuz

    The autoFillBackground widget autoFillBackground must be set to true.

    Now I tried to make a project from one form. Pasted your code into the constructor of this form. Everything works without autoFillBackground . It seems somewhere in the other dog rummaged.

      Good day!

      In your place, I would block the void QWidget::paintEvent(QPaintEvent*) drawing event at the widget and in it I would draw a picture using QPainter::drawImage() or QPainter::drawTiledPixmap() (depending on what you need) before loading it from resources. This method is used by Qt itself in the QSplashScreen widget. By the way, you can look in his direction, because from the code you cited, as I understand it, this is exactly what you need.

        I would advise: firstly convert the image to png, otherwise you will have to pull imageformats plug-ins for jpg behind the application. secondly use css. http://doc.crossplatform.ru/qt/4.5.0/stylesheet.html By the way, this method can also be used in the designer.