Movable QRubberband from one point to another - qt

I have drawn a QRubberband on QLabel. i can resize it using QSizeGrip. Now I want to move it from one point to another using QMouseevents. Is there any one who can help me out.
void CropImage::mousePressEvent(QMouseEvent *event)
{
QLabel::mousePressEvent(event);
lastPoint = event->pos();
rubberband = new QRubberBand(QRubberBand::Rectangle,this);
rubberband->setGeometry(QRect(lastPoint, QSize()));
rubberband->show();
}
void CropImage::mouseReleaseEvent(QMouseEvent *event)
{
newPoint = event->pos();
}
this is my subclass part which is used for mouse events. the code is as following:
Resizable_rubber_band::Resizable_rubber_band(QWidget *parent) : QWidget(parent)
{
//tell QSizeGrip to resize this widget instead of top-level window
setWindowFlags(Qt::SubWindow);
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
QSizeGrip* grip1 = new QSizeGrip(this);
QSizeGrip* grip2 = new QSizeGrip(this);
layout->addWidget(grip1, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(grip2, 0, Qt::AlignRight | Qt::AlignBottom);
rubberband = new QRubberBand(QRubberBand::Rectangle, this);
rubberband->move(0, 0);
rubberband->show();
}
void Resizable_rubber_band::resizeEvent(QResizeEvent *)
{
rubberband->resize(size());
}
void Resizable_rubber_band::mousePressEvent(QMouseEvent *event)
{
lastPoint = event->pos();
rubberband->childAt(lastPoint);
}
void Resizable_rubber_band::mouseReleaseEvent(QMouseEvent *event)
{
newpoint = event->pos();
int dragx=newpoint.x()-lastPoint.x();
int dragy=newpoint.y()-lastPoint.y();
band->move(0+dragx,0+dragy);
}
In this code, my problem is i am not getting the exact coordinates after dragging
thanks.
Ashish

Here is a quick example I made where you can move a QRubberBand using mouse events:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QRubberBand>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
private:
Ui::MainWindow *ui;
QRubberBand *rubberBand;
bool move_rubberband;
QPoint rubberband_offset;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
move_rubberband = false;
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(0,0,50,50);
rubberBand->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(rubberBand->geometry().contains(e->pos()))
{
rubberband_offset = e->pos() - rubberBand->pos();
move_rubberband = true;
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
if(move_rubberband)
{
rubberBand->move(e->pos() - rubberband_offset);
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
move_rubberband = false;
}

Related

(Qt)I want to know why the if statement won't be excuted

I want to make a simple timer by Qt. When I want to implement pause and resume functionality,it seemed that the if statement in void Widget::on_Pause_clicked()didn't work .
widget.h:
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QString>
#include <QLCDNumber>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
private:
Ui::Widget *ui;
QLCDNumber *lcd;
QTimer *ptime;
QTime *timerecord;
QPoint windowPos;
QPoint mousePos;
QPoint dPos;
bool isStart;
public:
Widget(QWidget *parent = nullptr);
~Widget();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private slots:
void on_Start_clicked();
void updatetime();
void initTime();
void on_Pause_clicked();
void on_Clear_clicked();
public slots:
};
widget.cpp:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ptime = new QTimer;
timerecord = new QTime;
ui->Timer->setDigitCount(11);
initTime();
connect(ptime,SIGNAL(timeout()),this,SLOT(updatetime()));
this->setWindowFlags(Qt::FramelessWindowHint);//remove system border
isStart=false;//determine if the timer is running
ui->Start->setEnabled(true);
ui->Pause->setEnabled(false);
ui->Clear->setEnabled(false);
}
Widget::~Widget()
{
delete ui;
}
void Widget::initTime()
{
timerecord->setHMS(0, 0, 0);
ui->Timer->display(timerecord->toString("mm:ss:zzz "));
}
void Widget::updatetime()
{
*timerecord = timerecord->addMSecs(1);
ui->Timer->display(timerecord->toString("mm:ss:zzz "));
}
void Widget::on_Start_clicked()//when the start button is clicked
{
ptime->start(1);
isStart=true;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(false);
}
void Widget::on_Pause_clicked()//when the pause button is clicked
{
if(isStart == true)
{
ptime->stop();
ui->Pause->setText("继续");
isStart=false;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(true);
}
if(isStart == false)
{
ptime->start(1);
ui->Pause->setText("暂停");
isStart=true;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(false);
}
}
void Widget::on_Clear_clicked()
{
ptime->stop();
initTime();
ui->Start->setEnabled(true);
ui->Pause->setEnabled(false);
ui->Clear->setEnabled(false);
}
void Widget::mousePressEvent(QMouseEvent *event)//make the window movable
{
this->windowPos = this->pos();
this->mousePos = event->globalPos();
this->dPos = mousePos - windowPos;
}
void Widget::mouseMoveEvent(QMouseEvent *event)//make the window movable
{
this->move(event->globalPos() - this->dPos);
}
In your code:
void Widget::on_Pause_clicked()//when the pause button is clicked
{
if(isStart == true)
{
ptime->stop();
ui->Pause->setText("继续");
isStart=false;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(true);
}
if(isStart == false)
{
ptime->start(1);
ui->Pause->setText("暂停");
isStart=true;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(false);
}
}
if isStart starts out to be true, then it enters the first if condition block. Within it, isStart becomes false.
However, when it exits that block, it hits the if (isStart == false) block and since isStart is false, therefore it goes into that block and changes isStart to true.
What you should do (bear in mind my qT is rusty):
void Widget::on_Pause_clicked()//when the pause button is clicked
{
if(isStart == true)
{
ptime->stop();
ui->Pause->setText("继续");
isStart=false;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(true);
} else {
ptime->start(1);
ui->Pause->setText("暂停");
isStart=true;
ui->Start->setEnabled(false);
ui->Pause->setEnabled(true);
ui->Clear->setEnabled(false);
}
}

How to change QPainterPath color after clicking on QPushButton

I subclassed a QPushButton so that I was able to re-implement the paintEvent(QPaintEvent *paint) method also advised by the official documentation.
Below is the sequence of operations:
a) After I launch the application the button is below:
b) This is after I hover on it:
c) then I click the button:
d) and finally I release the mouse
e) go away from the button
However the problem is that the QPainterPath with which I designed the green box it should be red after I release the button. And, of course, it should become green again after I click again the button.
Below the code:
custombutton.h
class CustomButton : public QPushButton
{
Q_OBJECT
public:
CustomButton(QWidget *parent = nullptr);
~CustomButton();
QString FirstName = "MACHINE";
QString LastName = "CONTROL";
protected:
void paintEvent(QPaintEvent *);
};
custombutton.cpp
CustomButton::CustomButton(QWidget *parent) : QPushButton(parent)
{
setGeometry(150, 150, 110, 110);
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet(
"QPushButton{background-color: lightGray;border: 1px solid black; border-radius: 5px;}"
"QPushButton:hover{background-color: gray;}"
"QPushButton:pressed{background-color: lightGray;}");
}
CustomButton::~CustomButton()
{
}
void CustomButton::paintEvent(QPaintEvent *paint)
{
QPushButton::paintEvent(paint);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QPainterPath path;
path.addRoundedRect(QRectF(15, 15, 80, 5), 2, 2);
QPen pen(Qt::black, 0.3);
p.setPen(pen);
p.fillPath(path, Qt::darkGreen);
p.drawPath(path);
p.save();
p.drawText(QPoint(20, 60), FirstName);
p.drawText(QPoint(20, 80), LastName);
p.setFont(QFont("Arial", 10));
p.restore();
}
mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void onClickedButton();
private:
Ui::MainWindow *ui;
CustomButton *newBtn;
};
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
newBtn = new CustomButton(this);
newBtn->show();
connect(newBtn, &QPushButton::clicked, this, &MainWindow::onClickedButton);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onClickedButton()
{
QPushButton* target = qobject_cast<QPushButton*>(sender());
if (target != nullptr)
{
QPainter p;
p.setRenderHint(QPainter::Antialiasing);
QPainterPath path;
path.addRoundedRect(QRectF(15, 15, 80, 5), 2, 2);
QPen pen(Qt::black, 0.3);
p.setPen(pen);
p.fillPath(path, Qt::darkRed);
p.drawPath(path);
p.save();
p.restore();
}
}
As you see in the MainWindow I created a function onClickedButton() which I thought would have triggered the QPainterPath into red after clicked. And in order to do that I casted it into an object (qobject_cast). But unfortunately it didn't work as expexted.
The solution is to create a flag that indicates the color and call update() for repainting:
#ifndef CUSTOMBUTTON_H
#define CUSTOMBUTTON_H
#include <QPushButton>
class CustomButton : public QPushButton
{
Q_OBJECT
public:
CustomButton(QWidget *parent = nullptr);
~CustomButton();
QString FirstName = "MACHINE";
QString LastName = "CONTROL";
private Q_SLOTS:
void handleClicked();
protected:
void paintEvent(QPaintEvent *);
private:
bool state;
};
#endif // CUSTOMBUTTON_H
#include "custombutton.h"
#include <QPainter>
#include <QPainterPath>
CustomButton::CustomButton(QWidget *parent) : QPushButton(parent), state(true)
{
setGeometry(150, 150, 110, 110);
setAttribute(Qt::WA_TranslucentBackground);
setStyleSheet(
"QPushButton{background-color: lightGray;border: 1px solid black; border-radius: 5px;}"
"QPushButton:hover{background-color: gray;}"
"QPushButton:pressed{background-color: lightGray;}");
connect(this, &QPushButton::clicked, this, &CustomButton::handleClicked);
}
CustomButton::~CustomButton()
{
}
void CustomButton::handleClicked()
{
state = !state;
update();
}
void CustomButton::paintEvent(QPaintEvent *paint)
{
QPushButton::paintEvent(paint);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
QPainterPath path;
path.addRoundedRect(QRectF(15, 15, 80, 5), 2, 2);
QPen pen(Qt::black, 0.3);
p.setPen(pen);
p.fillPath(path, state ? Qt::darkGreen: Qt::darkRed);
p.drawPath(path);
p.drawText(QPoint(20, 60), FirstName);
p.drawText(QPoint(20, 80), LastName);
p.setFont(QFont("Arial", 10));
}

Drawing a point in the exact position of a mouse click using Qt

I'm working on a Qt project. A point must be drawn on a mouse click on a Qpainter area. The point position is supposed to be on the same exact position of the mouse click, but for some reason the point is drawn in another position diagonal to the expected position.
The code :
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
QGraphicsView * view = new QGraphicsView(this) ;
ui->setupUi(this);
QGridLayout * gridLayout = new QGridLayout(ui->centralWidget);
gridLayout->addWidget(view);
scene = new QGraphicsScene();
scene->setSceneRect(50, 50, 350, 350);
view->setScene(scene);
}
void MainWindow::mousePressEvent(QMouseEvent * e)
{
QGraphicsView * view = new QGraphicsView(this) ;
double rad = 1;
QPointF pt = view->mapToScene(e->pos());
scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,QPen(), QBrush(Qt::SolidPattern));
}
Your code is not correct. You create heavy view every clicking, you should not do this. If you want that user will be able to interact with scene, then create new custom scene and do all hat you need in scene.
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QPoint>
#include <QMouseEvent>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
~GraphicsScene();
signals:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
public slots:
private:
};
#endif // GRAPHICSSCENE_H
cpp:
#include "graphicsscene.h"
#include <QDebug>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
}
GraphicsScene::~GraphicsScene()
{
qDebug() << "deleted scene";
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton)
{
double rad = 1;
QPointF pt = mouseEvent->scenePos();
this->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0,QPen(),
QBrush(Qt::SolidPattern));
}
QGraphicsScene::mousePressEvent(mouseEvent);
}
Usage, for example:
#include "graphicsscene.h"
//...
GraphicsScene *scene = new GraphicsScene(this);
someview->setScene(scene);

How to access a QLabel created in QtCreator Designer?

I created a few QLabel in my MainWindow using Qt Creator and opencv, but I have this error :
error: 'ui' was not declared in this scope
in the function filter_image(). I want to use label to show an image before and after doing some processing.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include<QImage>
#include<QLabel>
static QImage IplImage2QImage(const IplImage *iplImage) {
int height = iplImage->height;
int width = iplImage->width;
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
return img.rgbSwapped();
}
else if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 1)
{
const uchar *qImageBuffer = (const uchar*)iplImage->imageData;
QImage img(qImageBuffer, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++)
{
colorTable.push_back(qRgb(i, i, i));
}
img.setColorTable(colorTable);
return img;
}
else
{
std::cout << "Image cannot be converted.";
return QImage();
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void filter_image(IplImage* img) {
IplImage* roi = cvCreateImage(cvGetSize(img), 8, 3);
cvCvtColor( img, roi, CV_RGB2GRAY );
cvSmooth(img,roi,CV_BLUR, 5, 0, 0, 0);
cvThreshold(roi,roi,100,255,CV_THRESH_BINARY);
IplImage *img_de = cvCloneImage(roi);
QImage qt_i = IplImage2QImage(img_de);
QLabel label_2;
// display on label
ui->label_2->setPixmap(QPixmap::fromImage(qt_i));//error line
// resize the label to fit the image
ui->label_2->resize(ui->label_2->pixmap()->size());
label_2.show();
}
void MainWindow::on_actionOpen_triggered() {
IplImage *frame = cvLoadImage(
QFileDialog::getOpenFileName(this,
"Ouvrir un fichier",
"/../../Fichiers Image",
"Image (*.jpg *.bmp *.jpeg)")
.toStdString().c_str(),3);
IplImage *img_des = cvCloneImage(frame);
QImage qt_im = IplImage2QImage(img_des);
QLabel label;
// display on label
ui->label->setPixmap(QPixmap::fromImage(qt_im));
// resize the label to fit the image
ui->label->resize(ui->label->pixmap()->size());
label.show();
filter_image(frame);
}
mainwindow.h
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
public:
Ui::MainWindow *ui;
public slots:
void on_actionOpen_triggered();
};
ui_mainwindow.h
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow{
public:
QAction *actionOpen;
QWidget *centralWidget;
QLabel *label;
QLabel *label_2;
QMenuBar *menuBar;
QMenu *menuFile;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(600, 400);
actionOpen = new QAction(MainWindow);
actionOpen->setObjectName(QString::fromUtf8("actionOpen"));
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
label = new QLabel(centralWidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(290, 30, 271, 301));
label_2 = new QLabel(centralWidget);
label_2->setObjectName(QString::fromUtf8("label_2"));
label_2->setGeometry(QRect(20, 20, 281, 331));
MainWindow->setCentralWidget(centralWidget);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 600, 21));
menuFile = new QMenu(menuBar);
menuFile->setObjectName(QString::fromUtf8("menuFile"));
MainWindow->setMenuBar(menuBar);
mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);
menuBar->addAction(menuFile->menuAction());
menuFile->addAction(actionOpen);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
actionOpen->setText(QApplication::translate("MainWindow", "Open", 0, QApplication::UnicodeUTF8));
label->setText(QString());
label_2->setText(QString());
menuFile->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
} // retranslateUi};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
The method filter_image, which is where the error is occurring, is not a class method of MainWindow, so it doesn't have access to MainWindow's members (including ui).
You can do a couple of things: if it makes sense, make it part of the MainWindow class. Alternatively, you can pass the ui as a parameter:
void filter_image(Ui::MainWindow* ui, /*other params*/){...}
Also, you are making a local variable label_2 rather than taking the one from the gui.

Background image not showing on QWidget

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 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);
}
You can reimplement paintEvent:
void Widget::paintEvent( QPaintEvent* e )
{
QPainter painter( this );
painter.drawPixmap( 0, 0, QPixmap(":/new/prefix1/picture001.png").scaled(size()));
QWidget::paintEvent( e );
}

Resources