当前位置:网站首页>QT double buffer drawing
QT double buffer drawing
2022-04-23 12:15:00 【MechMaster】
Qt Double buffer drawing
1. Concept of double buffer
- Double buffered drawing is when drawing , First draw everything on a drawing device , And then the whole image is drawn on the part and displayed .
- Using double buffer drawing can avoid flicker when displaying .
- from Qt 4.0 Start ,QWidget All drawing of the part automatically uses double buffering , So generally there is no need to paintEvent() Function to avoid flicker .
- But to achieve some graphic effects , Or with the help of the concept of double buffer .
2. Application examples and code
- The following program realizes the function of drawing a rectangle of any size on the interface with the mouse .
- .h file
#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; // buffer
QPixmap tempPix; // Temporary buffer
QPoint startPoint;
QPoint endPoint;
bool isDrawing; // Are you drawing
protected:
void mousePressEvent(QMouseEvent * event);
void mouseMoveEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
void paintEvent(QPaintEvent * event);
};
#endif // WIDGET_H
- .cpp file
#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)
{
// Get the starting point of the rectangle
startPoint = event->pos();
isDrawing = true;
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
// For mouse movement events ,event->button() The return value is Qt::NoButton
//event->buttons() The return value is all the buttons pressed
//if(event->buttons()&Qt::LeftButton) // As long as the return value contains the left mouse button press , Just meet the requirements , For example, press the left and right mouse buttons at the same time
if(event->buttons()==Qt::LeftButton) // The return value can only contain one method of pressing the left mouse button , To meet the requirements
{
// Get the end point of the rectangle
endPoint = event->pos();
// Copy the contents of the buffer to the temporary buffer
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. elastic
- Qt Provided in QRubberBand Class to implement the rubber band .
版权声明
本文为[MechMaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231210423927.html
边栏推荐
- 智能多线弹性云增加独立的IP地址,如何实现多线功能?
- 1. Construction of electron development environment
- Tips for installing MySQL service in windows11: Install / Remove of the Service denied
- 电脑系统卡如何解决?
- C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
- The maximum number of remote desktop servers has been exceeded
- Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
- NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
- 【Redis 系列】redis 学习十三,Redis 常问简单面试题
- 获取钉钉考勤机打卡记录
猜你喜欢
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
In idea Solution to the problem of garbled code in Chinese display of properties file
网络信息安全之零信任
Tensorflow uses keras to create neural networks
同态加密技术学习
传统企业如何应对数字化转型?这些书给你答案
Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
Idea setting copyright information
随机推荐
Idea setting copyright information
第二十五课 类的静态成员变量
第四章 为IM 启用填充对象之为IM列存储启用ADO(IM 4.8)
User interface and im expression (IM 5.6)
Siri gave the most embarrassing social death moment of the year
Chapter 5 optimizing queries using in memory expressions (IM 5.1)
一个平面设计师的异想世界|ONES 人物
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
第四章 为IM 启用填充对象之启用和禁用表空间的IM列存储(IM 4.5)
NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
The maximum number of remote desktop servers has been exceeded
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
AI video cloud vs narrowband HD, who is the darling of the video era
How much does software testing help reduce program bugs?
How imeu is associated with imcu (IM 5.5)
画结果图推荐网址
软件测试基础DAY2-用例执行
Recommended programming AIDS: picture tool snipaste
亿级流量架构,服务器如何扩容?写得太好了!
C set Logo Icon and shortcut icon