tags:

views:

122

answers:

1

Hi,

I want to put a background image in mt QWidget, can anyone help me on this.

how to Ovveride, the paintevent of the Qwidget and draw a image there

+1  A: 

Hi. Here is what you need:

#include <QApplication>
#include <QGridLayout>
#include <QWidget>

int main(int argc, char ** argv)
{
    QApplication app( argc, argv );

    QWidget widget(0);
    widget.setStyleSheet("background-image: url(1.PNG)");

    QGridLayout *leftLayout = new QGridLayout();
    leftLayout->setSpacing (0);

    widget.setLayout(leftLayout);  

    widget.show(); 
    return app.exec();
}

Where 1.PNG is the image located in the same directory with the app.

mosg
Is there any other way that doesn't involve CSS (ideally one that uses embedded resources?)
advs89
(I'd submit my own question, but submitting similar questions is frowned upon)
advs89
@advs89 I guess you are talking about qrc file... So it's not a problem: *widget.setStyleSheet("background-image: url(:/images/1.PNG)");*, where *:/images/1.PNG* is a embedded resource file. After that you only have to add information about 1.PNG to the res.qrc file...
mosg
yeah thanks... exactly what I was looking for
advs89