当前位置:网站首页>Qt双缓冲绘图
Qt双缓冲绘图
2022-04-23 12:10:00 【MechMaster】
Qt双缓冲绘图
1. 双缓冲的概念
- 双缓冲绘图就是在绘制时,先将所有内容都绘制到一个绘图设备上,然后再将整个图像绘制到部件上显示出来。
- 使用双缓冲绘图可以避免显示时的闪烁现象。
- 从Qt 4.0开始,QWidget部件的所有绘制都自动使用了双缓冲,所以一般没有必要在paintEvent()函数中使用双缓冲代码来避免闪烁。
- 不过要想实现一些绘图效果,还是要借助双缓冲的概念。
2. 应用示例和代码
- 下面的程序实现使用鼠标在界面绘制一个任意大小的矩形的功能。

- .h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QPixmap pix; //缓冲区
QPixmap tempPix; //临时缓冲区
QPoint startPoint;
QPoint endPoint;
bool isDrawing; //是否正在绘图
protected:
void mousePressEvent(QMouseEvent * event);
void mouseMoveEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
void paintEvent(QPaintEvent * event);
};
#endif // WIDGET_H
- .cpp文件
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QPainter>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
pix = QPixmap(400,300);
pix.fill(Qt::white);
tempPix = pix;
isDrawing = false;
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
//获取矩形的开始点
startPoint = event->pos();
isDrawing = true;
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
//对于鼠标移动事件,event->button()返回值是Qt::NoButton
//event->buttons()返回值是所有按下的按钮
//if(event->buttons()&Qt::LeftButton) //返回值只要包含鼠标左键按下,就符合要求,比如鼠标左键和右键同时按下
if(event->buttons()==Qt::LeftButton) //返回值只能包含鼠标左键按下一种,才符合要求
{
//获取矩形的结束点
endPoint = event->pos();
//将缓冲区的内容复制到临时缓冲区
tempPix=pix;
update();
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
endPoint=event->pos();
isDrawing=false;
update();
}
}
void Widget::paintEvent(QPaintEvent *event)
{
int x=startPoint.x();
int y=startPoint.y();
int width=endPoint.x()-x;
int height=endPoint.y()-y;
QPainter painter;
painter.begin(&tempPix);
painter.drawRect(x,y,width,height);
painter.end();
painter.begin(this);
painter.drawPixmap(0,0,tempPix);
if(!isDrawing)
pix=tempPix;
}
3. 橡皮筋
- Qt中提供了QRubberBand类来实现橡皮筋。
版权声明
本文为[MechMaster]所创,转载请带上原文链接,感谢
https://liuhui.blog.csdn.net/article/details/124277629
边栏推荐
- 科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智
- Lesson 26 static member functions of classes
- IMEU如何与IMCU相关联(IM 5.5)
- 远程访问家里的树莓派(上)
- VMware virtual machines export hard disk vmdk files using esxi
- Force buckle - 1137 Nth teponacci number
- 传统企业如何应对数字化转型?这些书给你答案
- Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
- A detailed explanation of head pose estimation [collection of good articles]
- C set Logo Icon and shortcut icon
猜你喜欢

软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资

面了一圈,整理了这套面试题。。

编程辅助工具推荐:图片工具snipaste

力扣-1137.第N个泰波那契数
![[web daily practice] eight color puzzle (float)](/img/59/474080f6377b3684aa4fb2a39e1d81.png)
[web daily practice] eight color puzzle (float)

智能多线弹性云增加独立的IP地址,如何实现多线功能?

Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres

Basic software testing Day2 - Case Execution

C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism

【Redis 系列】redis 学习十三,Redis 常问简单面试题
随机推荐
Tensorflow uses keras to create neural networks
On using go language to create websocket service
用户接口和IM表达式(IM 5.6)
Optimize connections using connection groups (IM 6)
编程辅助工具推荐:图片工具snipaste
The listing of saiweidian Technology Innovation Board broke: a decrease of 26% and the market value of the company was 4.4 billion
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
同态加密技术学习
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
Force buckle - 70 climb stairs
Lesson 23 temporary objects
Lesson 24 analysis of classical problems
Castle.DynamicProxy实现事务单元控制
1.Electron开发环境搭建
C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
SQL exercise (I)
ImportError: libX11. so. 6: cannot open shared object file: No such file or directory
Master slave replication configuration of MySQL
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)