当前位置:网站首页>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
边栏推荐
- [web daily practice] eight color puzzle (float)
- 软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
- Solution of asynchronous clock metastability -- multi bit signal
- 同态加密技术学习
- Metalama简介4.使用Fabric操作项目或命名空间
- Tclerror: no display name and no $display environment variable
- Force buckle - 70 climb stairs
- NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
- C# F23. Stringsimilarity Library: String repeatability, text similarity, anti plagiarism
- [redis series] redis learning 13. Redis often asks simple interview questions
猜你喜欢
Docker MySQL master-slave backup
宝塔面板命令行帮助教程(包含重置密码)
Running error: unable to find or load the main class com xxx. Application
Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
Recommended programming AIDS: picture tool snipaste
Intelligent multi line elastic cloud adds independent IP address. How to realize multi line function?
A detailed explanation of head pose estimation [collection of good articles]
Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
随机推荐
Fastjson 2 来了,性能继续提升,还能再战十年
第二十六课 类的静态成员函数
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)
程序员如何用130行代码敲定核酸统计
第二十三课 临时对象
In idea Solution to the problem of garbled code in Chinese display of properties file
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
软件测试基础DAY2-用例执行
How the database fills in IM expressions (IM 5.4)
Force buckle - 70 climb stairs
What is a gateway
5个免费音频素材网站,建议收藏
Tips for installing MySQL service in windows11: Install / Remove of the Service denied
Array---
Idea database navigator plug-in
Metalama简介4.使用Fabric操作项目或命名空间
Win10 splash screen after startup
魔域来了H5游戏详细图文架设教程
对称加密、证书加密
How imeu is associated with imcu (IM 5.5)