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涂鸦板绘制的更多相关文章

  1. Android应用开发实例篇(1)-----简易涂鸦板

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378328.html 一.概述 这次要做一个简单的涂鸦板应用,以前在Qt上实现过,突然想 ...

  2. iOS_Quartz2D之涂鸦板

    响应者对象:继承了UIResponder的对象 触摸事件:一根或多根手指: 开始触摸: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent ...

  3. 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板

    [源码下载] 重新想象 Windows 8 Store Apps (51) - 输入: 涂鸦板 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 涂鸦板 通过 Poin ...

  4. 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板

    [源码下载] 背水一战 Windows 10 (60) - 控件(媒体类): Pointer 涂鸦板, InkCanvas 涂鸦板 作者:webabcd 介绍背水一战 Windows 10 之 控件( ...

  5. PPAPI+Skia实现的涂鸦板

    在PPAPI插件中使用Skia画图介绍了怎样在PPAPI中使用Skia,文末说回头要提供一个简单的涂鸦板插件,这次我来兑现承诺了. foruok原创,关注微信订阅号"程序视界"可联 ...

  6. HTML5实现涂鸦板

    原文:HTML5实现涂鸦板 最近闲的,看了看html5,强大的绘图功能让我惊奇,于是,写了个小玩意---涂鸦板,能实现功能有:画画,改色,调整画笔大小 html5的绘图可以分为点,线,面,圆,图片等, ...

  7. 实现简单的手写涂鸦板(demo源码)

    在一些软件系统中,需要用到手写涂鸦的功能,然后可以将涂鸦的结果保存为图片,并可以将"真迹"通过网络发送给对方.这种手写涂鸦功能是如何实现的了?最直接的,我们可以使用Windows提 ...

  8. Qt之自绘制饼图

    1.说明 最近在搞绘图方面的工作,说实话C++的第三方绘图库并不算多,总之我了解的有:qtcharts.ChartDirector.qwt.kdchart和QCustomPlot.这几个库各有利弊. ...

  9. QT 基本图形绘制

    QT 基本图形绘制 1.告诉绘制引擎一些东西 QPainter::Antialiasing 在可能的情况下,反锯齿       QPainter::TextAntialiasing 在可能的情况下,文 ...

随机推荐

  1. 使用sshkey连接github等服务器

    平常使用git时因为用了https的方式,所以经常要输入密码,其实我们是可以通过这个公钥连接github git.oschina.net等服务器,这样可以省去了我们输入用户名密码这么一个步骤了. 1. ...

  2. JavaScript正则表达式检验手机号码、邮箱、ip地址等

    1)检测IP地址的正则表达式 ((2 [0-4] \d | 25[0-5] | [01]?\d\d?) \.){3}(2 [0-4] \d | 25[0-5] | [01]?\d\d?) 2 [0-4 ...

  3. C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

    class Program { static void Main(string[] args) {//G:\zhyue\backup\projects\Test\ConsoleApplication1 ...

  4. dnspod域名解析设置

    time: 2016-01-8 10:30     因为我的博客是用Github page搭建的,所以要把域名和Github_name.github.io的URL联系起来.本人实在小白一个,就纪录一下 ...

  5. 热血沙城-3.2移植-古月-cocos2dx源码

    最近发现我去年学习2dx的时候移植过的一个游戏现在被放在网上出售 真是有点想笑 本人比较喜欢武侠风格的游戏,当时9秒开源了热血沙城 本着学习的态度 从2.1.2移植到3.2 用了一周的时间  中间各种 ...

  6. Eclipse 导入 Android studio Exception Ljava/lang/UnsatisfiedLinkEror

    android studio compile fileTree(dir: 'libs', include: ['*.jar']) 没有加载so文件 main 下加入 jniLibs---so文件即可 ...

  7. windows如何查看nvidia显卡(GPU)的利用率和温度

    windows如何查看nvidia显卡(GPU)的利用率和温度 nvidia-smi 只要在文件夹C:\Program Files\NVIDIA Corporation\NVSMI里找到文件nvidi ...

  8. 微信小程序“满月”:尝鲜之后你还用过它吗?

    距离 2017 年 1 月 9 日微信小程序上线,整整过去了一个月时间.和互联网时代每天出现的众多新鲜事物相似,小程序甫一诞生,立即占据了各大科技媒体网站头屏并引起社交圈的兴奋讨论.由于背靠微信,纷纷 ...

  9. JQuery 简单表格验证

    <form action="{% url 'register' %}" method="post"> 用户名:<input id=" ...

  10. 结对作业——随机生成四则运算(Core 第7组)

    结对作业 ——随机生成四则运算(core第7组) 吕佳玲 PB16060145 涂涵越 PB16060282 GITHUB地址 https://github.com/hytu99/homework_2 ...