初识Qt涂鸦板绘制
1、新建一个Qt Gui应用,项目名称为myPalette,基类选择为QMainWindow,类名设置为MainWindow。
2、在mainwindow.h头文件中添加以下代码,同时添加#include<QPushButton>
private:
Ui::MainWindow *ui;
QPixmap pix;
QPoint lastPoint;
QPoint endPoint;
qreal scale;
QPushButton *zoomInButton, *zoomOutButton; protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *); public slots:
void zoomIn();
void zoomOut();
3、在mainwindow.cpp文件中添加#include<QPainter>,同时在构造函数中添加以下代码段
resize(, );//窗口大小
pix = QPixmap(, );//画布大小
pix.fill(Qt::white); scale = ;//不放大
zoomInButton = new QPushButton(this);//放大按钮
zoomInButton->setText(tr("zoomIn"));
zoomInButton->move(,);
zoomOutButton = new QPushButton(this);//缩小按钮
zoomOutButton->setText(tr("zoomOut"));
zoomOutButton->move(,); connect(zoomInButton, SIGNAL(clicked()), this, SLOT(zoomIn()));
connect(zoomOutButton, SIGNAL(clicked()), this, SLOT(zoomOut()));
4、在mainwindow.cpp文件中配置相关的事件函数,代码如下
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter pp(&pix);
pp.drawLine(lastPoint/scale, endPoint/scale);//保证画布坐标和窗口坐标相同,避免窗口坐标变化,而画布坐标未发生改变
lastPoint = endPoint;
QPainter painter(this);
painter.scale(scale, scale);//进行放大操作,放大的是窗口坐标。如果想放大画布坐标,则执行pp.scale(scale, scale);
painter.drawPixmap(, , pix);//从窗口的原点(0, 0)添加该QPixmap对象,在窗口中放置画布pix。
//若drawPixmap(0, 0, 100, 100, pix),则指定了添加对象的尺寸大小
} void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)//button()函数返回的是按下的键,直接判断是否为某个键。适用于鼠标键一次按下
{
lastPoint = event->pos();
}
} void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton)//buttons()函数返回的是按下的键的状态,需要通过OR运算进行判断。适用于鼠标键持续按下
{
endPoint = event->pos();
update();//调用update()函数会进行paintEvent()函数的重新绘制
}
} void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
endPoint = event->pos();
update();
}
} void MainWindow::zoomIn()
{
scale *= ;//放大两倍
update();
} void MainWindow::zoomOut()
{
scale /= ;//缩小两倍
update();
}
5、运行结果如下

初识Qt涂鸦板绘制的更多相关文章
- Android应用开发实例篇(1)-----简易涂鸦板
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378328.html 一.概述 这次要做一个简单的涂鸦板应用,以前在Qt上实现过,突然想 ...
- iOS_Quartz2D之涂鸦板
响应者对象:继承了UIResponder的对象 触摸事件:一根或多根手指: 开始触摸: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent ...
- 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板
[源码下载] 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 涂鸦板 通过 Poin ...
- 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板
[源码下载] 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板 作者:webabcd 介绍背水一战 Windows 10 之 控件( ...
- PPAPI+Skia实现的涂鸦板
在PPAPI插件中使用Skia画图介绍了怎样在PPAPI中使用Skia,文末说回头要提供一个简单的涂鸦板插件,这次我来兑现承诺了. foruok原创,关注微信订阅号"程序视界"可联 ...
- HTML5实现涂鸦板
原文:HTML5实现涂鸦板 最近闲的,看了看html5,强大的绘图功能让我惊奇,于是,写了个小玩意---涂鸦板,能实现功能有:画画,改色,调整画笔大小 html5的绘图可以分为点,线,面,圆,图片等, ...
- 实现简单的手写涂鸦板(demo源码)
在一些软件系统中,需要用到手写涂鸦的功能,然后可以将涂鸦的结果保存为图片,并可以将"真迹"通过网络发送给对方.这种手写涂鸦功能是如何实现的了?最直接的,我们可以使用Windows提 ...
- Qt之自绘制饼图
1.说明 最近在搞绘图方面的工作,说实话C++的第三方绘图库并不算多,总之我了解的有:qtcharts.ChartDirector.qwt.kdchart和QCustomPlot.这几个库各有利弊. ...
- QT 基本图形绘制
QT 基本图形绘制 1.告诉绘制引擎一些东西 QPainter::Antialiasing 在可能的情况下,反锯齿 QPainter::TextAntialiasing 在可能的情况下,文 ...
随机推荐
- Implementation:Bellman-ford
单源最短路径算法Bellman-ford练习,可以处理有负边的情况(也可以在存在负圈时及时终止) #include <iostream> #include <cstdlib> ...
- [h5+api]移动app开发用到的微信好友,朋友圈,qq好友,新浪微博分享合集
适用H5+环境,能够使用plus方法的移动app中 /** * Created by HBuilder. * User: tyx * Date: 2018-11-21 * Time: 17:28:51 ...
- OpenGL学习—04--彩色立方体
1.tutorial04.cpp // Include standard headers #include <stdio.h> #include <stdlib.h> ...
- B/S与C/S架构、B/S架构协议
软件有三大类型 单机类型.CS类型(Outlook.QQ.大型游戏).BS类型 BS结构中的协议 在BS结构中,首先使用到DNS协议:网络传输部分使用TCP/IP参考模型,其中网络接入层没有相应协议, ...
- Linux学习之CentOS(一)----在VMware虚拟机中安装CentOS 7
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- C++虚函数原理
类中的成员函数分为静态成员函数和非静态成员函数,而非静态成员函数又分为普通函数和虚函数. Q: 为什么使用虚函数 A: 使用虚函数,我们可以获得良好的可扩展性.在一个设计比较好的面向对象程序中,大多数 ...
- js判断状态
'<input type="radio" class="danxuan" name="danxuan" code="'||v ...
- C# 输入一个整数,求质因数
质数,质因数 应该都了解,在这里不过多解释,直接上代码: List<int> results = new List<int>(); int number = Int32.Par ...
- 第一个 mac 程序 Create-JSON-Model
第一个 mac 程序 Create-JSON-Model 效果图 数据 {"ID":null,"name":"Doe","firs ...
- 一个好玩的计算题目(c++ 位运算)
2015/11/10 在一个qq群里面,看到一个好玩的题目.“int foo(int x){return x&-x} foo(2^31-3)这个怎么算 ?” 1.自己也就开始算了: (1) ...