初识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 在可能的情况下,文 ...
随机推荐
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- js中list 和 map还有string的部分操作
1.创建list或者数组 var list = []; list中添加元素:list.push("hello"); 如果没有先定义为数组类型不能使用 push方法 判断list ...
- css3动画运用
https://daneden.github.io/animate.css/ https://minimamente.com/example/magic_animations/ http://i ...
- git 错误error: failed to push some refs to
今天使用VSCODE 学习node.js, 想在git上push代码 于是在git上建立了一个私有的长裤, 连接后push代码时提示如下错误: error: failed to push some ...
- Pig数据类型
基本类型 int.long.float.double.chararray.bytearray.datatime.boolean.biginteger.bigdecimal 复杂类型 map.tuple ...
- Html 表单标签 Form
Html表单 #转载请留言联系 表单用于搜集不同类型的用户输入,表单由不同类型的标签组成,相关标签及属性用法如下: 1.<form>标签 定义整体的表单区域 action属性 定义表单数据 ...
- SSH入门常用命令
一.参考链接大猫的博客
- The directory '/home/stone/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If execu
使用sudo pip install ......的时候出现下面一段黄色的代码: The directory '/home/stone/.cache/pip/http' or its parent d ...
- ASP.NET Web Api vs Node.js Benchmark
http://mikaelkoskinen.net/post/asp-net-web-api-vs-node-js-benchmark ASP.NET Web Api vs Node.js Bench ...
- 【C#】#100 调用摄像头
需求:由于项目需要获得用户的头像,所以需要用C#调用摄像头获取头像. 下面写一个调用摄像头的方法 案例:调用摄像头的一个DEMO[效果图] 使用的类库:AForge.dll [Demo下载,Dem ...