#ifndef _MY_WIDGET_H
#define _MY_WIDGET_H
#include <qapplication.h>
#include <qpushbutton.h>
#include <qpainter.h>
#include <qpixmap.h>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent = 0, const char* name = 0);
~MyWidget();
// to render the image onto the widget
void paintImage();
/* paint handler */
void paintEvent(QPaintEvent* e);
/* mouse event handler */
void mousePressEvent(QMouseEvent* e);
// TODO: Add the mouse move and mouse release event handlers
public slots:
// to save the image into a bitmap file
void saveImage();
private:
QPainter* painter;
QPixmap* image;
QPushButton* saveButton;
// for storing the size of the margin to the top and the left edge of the window
int margin_x, margin_y;
// for storing the mouse press position
int prev_x, prev_y;
};
#endif
this is my widget.h file
#include "my_widget.h"
#include <qpen.h>
#include <qbrush.h>
#include <iostream>
using namespace std;
/************************************/
// Constructor & Destructor
/************************************/
MyWidget::MyWidget(QWidget* parent, const char* name) : QWidget(parent, name) {
// create the QPainter for the widget
painter = new QPainter(this);
// create a pixmap image
image = new QPixmap(400, 300);
// fill the image with white color
image->fill(Qt::white);
// set the margin size
margin_x = 50;
margin_y = 50;
// create the push button to save the image into a bitmap file
saveButton = new QPushButton("Save Image", this);
QObject::connect(saveButton, SIGNAL(clicked()), this, SLOT(saveImage()));
saveButton->setGeometry(150, 370, 200, 30);
// set the size of the window
resize(500, 450);
}
MyWidget::~MyWidget(){
delete painter;
delete image;
delete saveButton;
}
/************************************/
// Mouse Event Handlers
/************************************/
// Mouse Press Event Handler
void MyWidget::mousePressEvent(QMouseEvent * e) {
// check if the button that caused the event the right button of the mouse
if ( e->button() == Qt::RightButton ) {
// copy the existing pixmap image back to the widget
paintImage();
// create a QPen object with black line color, width size of 5 and solid line style
QPen pen(Qt::black, 5, Qt::SolidLine);
// create a QBrush object with blue fill color, and solid pattern fill style
QBrush brush(Qt::blue, Qt::SolidPattern);
// close the active painter
if ( painter->isActive() )
painter->end();
// begins painting the widget
painter->begin(this);
painter->setPen(pen); // set the pen of the painter
painter->setBrush(brush); // set the brush of the painter
painter->setClipRect(margin_x, margin_y, 400, 300); // enable drawing with a rectangular area only
painter->drawRect(e->x(), e->y(), 80, 120); // draw a rectangle of size 80 by 120
painter->end();
// begins painting the pixmap image
painter->begin(image);
painter->setPen(pen); // set the pen of the painter
painter->setBrush(brush); // set the brush of the painter
painter->drawRect(e->x() - margin_x, e->y() - margin_y, 80, 120); // draw a rectangle of size 80 by 120
painter->end();
} else if ( e->button() == Qt::LeftButton ) {
// TODO: implement the actions for mouse left button press
}
}
// Mouse Move Event Handler
// TODO: implement the mouse move event handler
// Mouse Release Event Handler
// TODO: implement the mouse release event handler
/************************************/
// Paint Event Handler
/************************************/
void MyWidget::paintEvent(QPaintEvent *) {
// copy the existing pixmap image back to the widget
paintImage();
}
/************************************/
// Member Functions
/************************************/
// paintImage() function is to copy the existing pixmap image back to the widget
void MyWidget::paintImage() {
// close the active painter
if ( painter->isActive() ) {
painter->end();
}
// begin drawing on the widget
painter->begin(this);
// draw the pixmap from the image QPixmap object to the widget
if ( !image->isNull() ) {
painter->drawPixmap(margin_x, margin_y, (*image));
}
painter->end();
}
// paintImage() function is to save the pixmap image into a bitmap image file, named "temp.bmp"
void MyWidget::saveImage() {
image->save("temp.bmp", "BMP");
}
this is my widget.cpp
I am really new to programming in c++ and Qt
I have to modify the my_widget.h and my_widget.cpp such that the user can draw a rectangle in different sizes by mouse drag. Hence, when a user clicks and drags the left mouse button, a rectangle will be drawn until the user releases the button. The size of the rectangle will depend on how far the user drags the mouse.
I really don't understand how to start. can some1 help me with this ?