当前位置:网站首页>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
边栏推荐
- Database Navigator 使用默认MySQL连接提示:The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or repres
- 初探 Lambda Powertools TypeScript
- 电脑系统卡如何解决?
- Castle. Dynamic proxy implements transaction unit control
- Siri gave the most embarrassing social death moment of the year
- AI 视频云 VS 窄带高清,谁是视频时代的宠儿
- IM表达式的目的(IM 5.2)
- Tan Xiang, CEO of Kechuang · Pera software: the essence of zero trust is digital security. To B should also deeply study the user's mind
- Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
- PSCP basic usage
猜你喜欢

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

IDEA设置版权信息

电脑系统卡如何解决?

Win10 splash screen after startup

Qt进程间通信

九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request

远程桌面之终端服务器超出了最大允许连接数解决

NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol

worder字体网页字体对照表

On lambda powertools typescript
随机推荐
Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
Share two practical shell scripts
Lesson 24 analysis of classical problems
一个平面设计师的异想世界|ONES 人物
为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
1.Electron开发环境搭建
Lesson 25 static member variables of classes
为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
Force buckle - 70 climb stairs
Worder font page font comparison table
How the database fills in IM expressions (IM 5.4)
Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
Solution of asynchronous clock metastability -- multi bit signal
一个平面设计师的异想世界|ONES 人物
第二十三课 临时对象
Qt一个进程运行另一个进程
电脑系统卡如何解决?
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
同态加密技术学习
What is a gateway