当前位置:网站首页>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
边栏推荐
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- Step function of activation function
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
- Basic software testing Day2 - Case Execution
- Idea setting copyright information
- Lesson 23 temporary objects
- Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
- 关于使用Go语言创建WebSocket服务浅谈
- Chapter 4 specifies the attribute of the inmemory column on the no inmemory table for im enabled filling objects: examples (Part IV of im-4.4)
- Use kettle to copy records to and get records from results
猜你喜欢

Running error: unable to find or load the main class com xxx. Application

程序员如何用130行代码敲定核酸统计

IDEA 中 .properties文件的中文显示乱码问题的解决办法

Tips for installing MySQL service in windows11: Install / Remove of the Service denied

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

In idea Solution to the problem of garbled code in Chinese display of properties file

Sofa weekly | excellent Committee of the year, contributor of this week, QA of this week

Next.js 静态数据生成以及服务端渲染的方式

Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?

Next. JS static data generation and server-side rendering
随机推荐
编程辅助工具推荐:图片工具snipaste
IDEA 中 .properties文件的中文显示乱码问题的解决办法
Array---
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
Tensorflow uses keras to create neural networks
第四章 为IM 启用填充对象之启用和禁用列(IM-4.3 第三部分)
Next. JS static data generation and server-side rendering
远程桌面之终端服务器超出了最大允许连接数解决
User interface and im expression (IM 5.6)
Idea database navigator plug-in
Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
Siri gave the most embarrassing social death moment of the year
IM 体系结构:CPU架构:SIMD向量处理(IM-2.3)
What is a gateway
画结果图推荐网址
worder字体网页字体对照表
Step function of activation function
第四章 为IM 启用填充对象之强制填充In-Memory对象:教程(IM 4.7)
Share two practical shell scripts
激活函数之relu函数