初识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 在可能的情况下,文 ...
随机推荐
- window平台安装node.js绿色版
1. 下载Windows Binary版本的node.js文件 2. 把下载的ZIP压缩包解压到某个目录下(例如:D:\nodejs) 3. 此时的node.js只在刚刚解压的目录才起作用,需要配置环 ...
- CSS响应式:根据分辨率加载不同CSS的几个方法,亲测可用
有时候你需要把同一个页面在手机和pc同时打开,其中有一个办法就是判断不同分辨路加载不同的css 小编总结了几种分别加载css的方法: 1.比较复杂的使用js判断加载不同css (亲测可用) 但是这种方 ...
- PHP中的prepare准备语句的意义
mysqli和PDO扩展都有prepare这个语法,刚开始以为只是单纯的语法,没想到,还是有实际意义的: “每次发送查询语句给MySQL服务时,都必须解析该查询的语法,确保结构正确并能够执行.这是这个 ...
- css类选择器类名覆盖优先级
code <style> .a{ background: red; } .b{ background: yellow; } </style> <div class=&qu ...
- 一起来学习android自定义控件—边缘凹凸的View
1前言 最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义view方面的知识比较薄弱,所以想试试用自定义View来实现.但是由于自己知识点薄弱,一开 ...
- 2cmd 窗口 javac 错误:编码GBK的不可映射字符
错误截图: 解决办法:第一步 第二步:
- java 对象
对象可以看成是静态属性和动态属性的封装体.静态属性——成员变量:动态属性——方法. 1.汇编语言是对机器语言的抽象. 2.面向过程的语言是对汇编语言的抽象.属性和方法分离,不是封装在一起的,复用性 ...
- 让索引包含null值的两种方法
1. 把有NULL值的列与一个常数,或者一个带有not null约束的列一同索引 create index ind_01 on t01(col01,1); 或者 create index ind_01 ...
- springboot学习入门之三---启动原理
3启动原理 3.1启动类 @SpringBootApplication public class Application { public static void main(String[] args ...
- leetCode题解 寻找运动环
1.题目描述 Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this ...