初识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 在可能的情况下,文 ...
随机推荐
- 小tip: 某简单的字符重叠与图形生成----张鑫旭
引言 字符重叠不是什么稀奇的东西. 如1像素错位模拟阴影效果: 或者powerFloat中展示的带边框三角: 以及其他很多. 但是技术这东西不是豆腐,老了可以吃,臭了也可以吃:那我这里还拿着个说事作甚 ...
- Vue 框架-10-搭建脚手架 CLI
Vue 框架-10-搭建脚手架 CLI + 批处理快捷启动 脚手架是通过 webpack 搭建的开发环境 使用 ES6 语法 打包和压缩 JS 为一个文件 项目文件在环境中,而不是浏览器 实现页面自动 ...
- linux yum 安装wget、gcc、ifconfig、vim、setup
安装wgetyum -y install wget安装gcc c语言编译器yum -y install gcc安装ifconfigyum -y install net-tools.x86_64安装vi ...
- node(6)angular介绍
一.angular 的介绍 AngularJS[1] 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中. ...
- LeetCode 题解之Find Peak Element
1.题目描述 2.题目分析 在数组的首尾各加入INT_MIN ,然后遍历数组. 3.代码 int findPeakElement(vector<int>& nums) { ) ; ...
- selenium&PhantomJS笔记
配置pip文件 Windows下pip 配置文件的位置%HOME%/pip/pip.ini linux下安装pip,以Debian Linux为例su -apt-get install python- ...
- java笔记--关于多线程如何查看JVM中运行的线程
查看JVM中的线程 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3890280.html "谢谢-- ThreadGrou ...
- mysqlbinlog usage
[root@localhost mysql3306]# mysqlbinlogmysqlbinlog Ver 3.4 for el7 at x86_64Copyright (c) 2000, 2018 ...
- Man's Best Friend: The Science Behind the Dog and Human Relationship
http://info.thinkfun.com/stem-education/mans-best-friend-the-science-behind-the-dog-and-human-relati ...
- Linux uname命令详解
uname常见命令参数 -a, --all print all information, in the following order, except omit -p and -i if unknow ...