tags:

views:

506

answers:

2

HI,

i am designing a window using QWidget and set a background image, when i run my code i am not getting background image but showing window with default background.

Can anyone help me what may be the reason.

+2  A: 

there are many ways to set the background color to window,

i will give you one simple technique. i.e Ovveride, the paintevent of the Qwidget. and draw the pixmap there.

Here is the sample widget code, i hope it helps

Header file

#ifndef QBACKGROUNDIMAGE_H
#define QBACKGROUNDIMAGE_H

#include <QtGui/QMainWindow>
#include "ui_QbackgroundImage.h"
#include <QtGui>
class backgroundImgWidget ;
class QbackgroundImage : public QMainWindow
{
    Q_OBJECT

public:
    QbackgroundImage(QWidget *parent = 0);
    ~QbackgroundImage();

private:
    Ui::QbackgroundImage ui;


};

class backgroundImgWidget : public QWidget
    {
     Q_OBJECT

     public:
     backgroundImgWidget(QWidget *parent = 0);
     ~backgroundImgWidget();

     protected:
     void paintEvent(QPaintEvent *p2);
    };
#endif // QBACKGROUNDIMAGE_H

CPP file

#include "QbackgroundImage.h"

QbackgroundImage::QbackgroundImage(QWidget *parent)
    : QMainWindow(parent)
{
    //ui.setupUi(this);

        backgroundImgWidget* widget = new backgroundImgWidget();
        setCentralWidget(widget);
}

QbackgroundImage::~QbackgroundImage()
{

}

backgroundImgWidget::backgroundImgWidget(QWidget *parent):QWidget(parent)
    {

    }
backgroundImgWidget::~backgroundImgWidget()
    {

    }

void backgroundImgWidget::paintEvent(QPaintEvent *p2)
        {

                QPixmap pixmap;

            pixmap.load(":/new/prefix1/Sunset.jpg");

            QPainter paint(this);
            paint.drawPixmap(0, 0, pixmap);
            QWidget::paintEvent(p2);
        }
Shadow
Can u give some sample or link , i am new to this domain
Thanks a lotI compiled without any error but problem is not getting background image.I declared the same function in classFor virtual function is it needed to declare Any need to use update function
add the image to your .qrc file.create a sample qrc file, add the image over there. set the path to pix-map..
Shadow
my image file is already there in .QRC file. i checked it in QT designer also..... How to call paintEvent() function
have you inherited the class with Qwidget?paintevent is a virtual function, it should come automaitcally..keep break point and see control is coming or not?have created the object of backgroundImgWidget in mainwindow class?
Shadow
Yes i inherited Qwidget..... My constructor is... STUDY(QWidget *parent = 0, Qt::WFlags flags = 0);
i will show my code
A: 
// In header file
class STUDY : public QMainWindow, public Ui::STUDYClass
{
Q_OBJECT
public:
STUDY(QWidget *parent = 0, Qt::WFlags flags = 0);
~STUDY();
QPaintEvent *p2;
void backgroundImage();
void paintEvent(QPaintEvent *);
public slots:
};

//Constructor and paintEvent function in Cpp file

STUDY::STUDY(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
 setupUi(this);
 backgroundImage();
 update();
 paintEvent(p2);
}
void STUDY::paintEvent(QPaintEvent *p2)
{
    QPixmap pixmap;
    pixmap.load(":/STUDY/Resources/Homepage.png");
    QPainter paint(this);
    paint.drawPixmap(0, 0, pixmap);
    QWidget::paintEvent(p2);
}
Check my answer above, i have updated it.the way you are doing is wrong, paint event is a virtual function and it will get called automatically, when the paint event occurs
Shadow
Thanks a lot.. Now i got the answer...After removing : in :/STUDY/Resources/Homepage.png i got answer... i gave liek this /STUDY/Resources/Homepage.png