1 塗鴉板函數
就是事先設置畫板,經過畫圖Qpainter 進行圖形繪製,而後根據鼠標的軌跡進行繪製Point 而後調用upate進行圖形更新從而實現塗鴉板ui
2 講解源碼 this
#include "mainwindow.h"
#include "ui_mainwindow.h"get
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);源碼
resize(600,500);
pix=QPixmap(200,200);
pix.fill(Qt::white);
scale=1;it
//放大按鈕
btn=new QPushButton(this);
btn->setText(tr("scale"));event
//按鈕位置
btn->move(500,450);ast
//槽機制消息
QObject::connect(btn,SIGNAL(clicked(bool)),this,SLOT(ZooIn()));
}cli
MainWindow::~MainWindow()
{
delete ui;
}date
void MainWindow::paintEvent(QPaintEvent *){
//pix 在頭文件中定義 pix的大小就是板子的大小
QPainter paint(&pix);
paint.drawLine(lastpoint,endpoint);
lastpoint=endpoint;
QPainter paint2(this);
// 改變大小 是經過 update 中 調用 paintEvent函數實現圖形更新的
paint2.scale(scale,scale);
paint2.drawPixmap(0,0,pix);
}
void MainWindow::mousePressEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton)
lastpoint=event->pos();
endpoint=lastpoint;
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event){
if(event->button()==Qt::LeftButton)
{
endpoint=event->pos();
update();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
if(event->buttons()&Qt::LeftButton)
{
endpoint=event->pos();
update();
}
}
//消息處理 void MainWindow::ZooIn(){ scale*=2; update(); }