天天看點

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 塗鴉闆