天天看点

Qt 涂鸦板

<pre name="code" class="cpp">
           
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;"> </span>DIALOG_H
           
#define DIALOG_H      
#include <QDialog>      
#include <QMouseEvent>      
#include <QPushButton>      
namespace Ui {      
class Dialog;      
}      
class Dialog : public QDialog      
{      
Q_OBJECT      
public:      
explicit Dialog(QWidget *parent = 0);      
~Dialog();      
private:      
Ui::Dialog *ui;      
QPixmap pix;      
QPoint lastPoint;      
QPoint endPoint;      
qreal scale;      
QPushButton *button;      
QPixmap tempPix; //辅助画布      
bool isDrawing;   //标志是否正在绘图      
protected:      
void paintEvent(QPaintEvent *);      
void mousePressEvent(QMouseEvent *);      
void mouseMoveEvent(QMouseEvent *);      
void mouseReleaseEvent(QMouseEvent *);      
private slots:      
void zoomIn();      
};      
#endif // DIALOG_H      
#include "dialog.h"      
#include "ui_dialog.h"      
#include <QPainter>      
Dialog::Dialog(QWidget *parent) :      
QDialog(parent),      
ui(new Ui::Dialog)      
{      
ui->setupUi(this);      
resize(600, 500);    //窗口大小设置为600*500      
pix = QPixmap(200, 200);      
pix.fill(Qt::white);      
isDrawing = false;      
//设置初始放大倍数为1,即不放大      
scale =1;      
//新建按钮对象      
button = new QPushButton(this);      
//设置按钮显示文本      
button->setText(tr("zoomIn"));      
//设置按钮放置位置      
button->move(500, 450);      
//对按钮的单击事件和其槽函数进行关联      
connect(button, SIGNAL(clicked()), this, SLOT(zoomIn()));      
}      
Dialog::~Dialog()      
{      
delete ui;      
}      
void Dialog::paintEvent(QPaintEvent *)      
{      
#if 0      
if(scale!=1) //如果进行放大操作      
{      
//临时画布,大小变化了scale倍      
QPixmap copyPix(pix.size()*scale);      
QPainter pter(&copyPix);      
pter.scale(scale, scale);      
//将以前画布上的内容复制到现在的画布上      
pter.drawPixmap(0, 0, pix);      
//将放大后的内容再复制回原来的画布上      
pix = copyPix;      
//让scale重新置1      
scale =1;      
}      
QPainter pp(&pix);    // 根据鼠标指针前后两个位置就行绘制直线      
//pp.drawLine(lastPoint, endPoint); // 让前一个坐标值等于后一个坐标值,这样就能实现画出连续的线      
pp.drawLine(lastPoint/scale, endPoint/scale);      
lastPoint = endPoint;      
QPainter painter(this);      
//进行放大操作      
painter.scale(scale, scale);      
painter.drawPixmap(0, 0, pix);      
#endif      
int x,y,w,h;      
x = lastPoint.x();      
y = lastPoint.y();      
w = endPoint.x() - x;      
h = endPoint.y() - y;      
QPainter painter(this);      
if(isDrawing) //如果正在绘图,就在辅助画布上绘制      
{      
//将以前pix中的内容复制到tempPix中,保证以前的内容不消失      
tempPix = pix;      
QPainter pp(&tempPix);      
pp.drawRect(x,y,w,h);      
painter.drawPixmap(0, 0, tempPix);      
} else {      
QPainter pp(&pix);      
pp.drawRect(x,y,w,h);      
painter.drawPixmap(0,0,pix);      
}      
}      
void Dialog::mousePressEvent(QMouseEvent *event)      
{      
if(event->button()==Qt::LeftButton) //鼠标左键按下      
{      
lastPoint = event->pos();      
isDrawing = true;   //正在绘图      
}      
}      
void Dialog::mouseMoveEvent(QMouseEvent *event)      
{      
if(event->buttons()&Qt::LeftButton) //鼠标左键按下的同时移动鼠标      
{      
endPoint = event->pos();      
update(); //进行绘制      
}      
}      
void Dialog::mouseReleaseEvent(QMouseEvent *event)      
{      
if(event->button() == Qt::LeftButton) //鼠标左键释放      
{      
endPoint = event->pos();      
isDrawing = false;    //结束绘图      
update();      
}      
}      
void Dialog::zoomIn()      
{      
scale *=2;      
update();      
}      
Qt 涂鸦板
Qt 涂鸦板