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.
// 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);
}
There are many ways to set the background color to window,
I will give you one simple technique. i.e Override, the
paintEventof theQWidget. and draw the pixmap there.Here is the sample widget code, i hope it helps
Header file
CPP file