qt repaint 用法_关于Qt 中update()和repaint()的区别
void QWidget::repaint ( int x, int y, int w, int h, bool erase = TRUE ) [槽]
通过立即调用paintEvent()来直接重新绘制窗口部件,如果erase为真,Qt在paintEvent()调用之前擦除区域(x,y,w,h)。
如果w是负数,它被width()-x替换,并且如果h是负数,它被height()-y替换。 如果你需要立即重新绘制,建议使用repaint(),
比如在动画期间。在绝大多数情况下,update()更好,因为它允许Qt来优化速度并且防止闪烁。
警告:如果你在一个函数中调用repaint(),而它自己又被paintEvent()调用,你也许会看到无线循环。
update()函数从来不会产生循环。
void QWidget::update () [槽]
更新窗口部件,当Qt回到主事件中时,它规划了所要处理的绘制事件。这样允许Qt进行优化从而得到比调用repaint()更快的速度和
更少的闪烁。 几次调用update()的结果通常仅仅是一次paintEvent()调用。 Qt通常在paintEvent()调用之前擦除这个窗口部件的
区域,仅仅只有在WRepaintNoErase窗口部件标记被设置的时候才不会。
在这区别中关键点是:repaint()是立即调用paintEvent(),而update()是几次执行才调用一次paintEvent()。
这样update()会造成这样的结果:paintEvent()中的任务没有执行完,就又被update().paintEvent()中被积压的任务越来越多。
程序例子:
(1)问题出现时候的情况(10毫秒每次,用update()。paintEvent()积累了很多处理任务):
#include
#include
#include
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->showMaximized();
i = 0;
realWidth = this->width();
realHeight = this->height();
pixmap = QPixmap(realWidth,realHeight);
connect(this,SIGNAL(haveData(QPoint)),this,SLOT(getPointAndDraw(QPoint)));
connect(&timer,SIGNAL(timeout()),this,SLOT(getPoint()));
timer.start(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::getPoint()
{
if(i < realWidth)
{
point = QPoint(i,(uint(qrand())) % realHeight);
i++;
}
else
{
i = i % realWidth;
point = QPoint(i,(uint(qrand())) % realHeight);
i++;
}
emit haveData(point);
}
void MainWindow::getPointAndDraw(QPoint point)
{
index = point.x();
QPainter painter(&pixmap);
painter.setPen(Qt::green);
painter.drawLine(lastPoint,point);
painter.setPen(Qt::black);
painter.setBrush(Qt::red);
painter.drawRect(index+1,0,5,realHeight);
if(point.x() < realWidth-1)
lastPoint = point;
else
lastPoint = QPoint(0,0);
update();
// this->repaint(index-1,0,5,realHeight);
}
void MainWindow::paintEvent(QPaintEvent *e)
{
//return ;
QPainter painter(this);
QRect target1(0, 0, realWidth, realHeight/5);
QRect target2(0, realHeight/5, realWidth, realHeight/5);
QRect target3(0, 2*realHeight/5, realWidth, realHeight/5);
QRect target4(0, 3*realHeight/5, realWidth, realHeight/5);
QRect target5(0, 4*realHeight/5, realWidth, realHeight/5);
QRect source(0, 0, realWidth, realHeight);
painter.drawPixmap(target1,pixmap,source);
painter.drawPixmap(target2,pixmap,source);
painter.drawPixmap(target3,pixmap,source);
painter.drawPixmap(target4,pixmap,source);
painter.drawPixmap(target5,pixmap,source);
}
void MainWindow::resizeEvent(QResizeEvent *e)
{
realWidth = this->width();
realHeight = this->height();
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
(2)每隔1000毫秒刷新一次,用update().一秒种有足够的时间处理paintEvent(),无积累。
#include
#include
#include
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->showMaximized();
i = 0;
realWidth = this->width();
realHeight = this->height();
pixmap = QPixmap(realWidth,realHeight);
connect(this,SIGNAL(haveData(QPoint)),this,SLOT(getPointAndDraw(QPoint)));
connect(&timer,SIGNAL(timeout()),this,SLOT(getPoint()));
timer.start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::getPoint()
{
if(i < realWidth)
{
point = QPoint(i,(uint(qrand())) % realHeight);
i++;
}
else
{
i = i % realWidth;
point = QPoint(i,(uint(qrand())) % realHeight);
i++;
}
emit haveData(point);
}
void MainWindow::getPointAndDraw(QPoint point)
{
index = point.x();
QPainter painter(&pixmap);
painter.setPen(Qt::green);
painter.drawLine(lastPoint,point);
painter.setPen(Qt::black);
painter.setBrush(Qt::red);
painter.drawRect(index+1,0,5,realHeight);
if(point.x() < realWidth-1)
lastPoint = point;
else
lastPoint = QPoint(0,0);
update();
// this->repaint(index-1,0,5,realHeight);
}
void MainWindow::paintEvent(QPaintEvent *e)
{
//return ;
QPainter painter(this);
QRect target1(0, 0, realWidth, realHeight/5);
QRect target2(0, realHeight/5, realWidth, realHeight/5);
QRect target3(0, 2*realHeight/5, realWidth, realHeight/5);
QRect target4(0, 3*realHeight/5, realWidth, realHeight/5);
QRect target5(0, 4*realHeight/5, realWidth, realHeight/5);
QRect source(0, 0, realWidth, realHeight);
painter.drawPixmap(target1,pixmap,source);
painter.drawPixmap(target2,pixmap,source);
painter.drawPixmap(target3,pixmap,source);
painter.drawPixmap(target4,pixmap,source);
painter.drawPixmap(target5,pixmap,source);
}
void MainWindow::resizeEvent(QResizeEvent *e)
{
realWidth = this->width();
realHeight = this->height();
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
(3)继续改进(10毫秒每次,用repaint()。一次repaint(),一次paintEvent(),无积累).
#include
#include
#include
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->showMaximized();
i = 0;
realWidth = this->width();
realHeight = this->height();
pixmap = QPixmap(realWidth,realHeight);
connect(this,SIGNAL(haveData(QPoint)),this,SLOT(getPointAndDraw(QPoint)));
connect(&timer,SIGNAL(timeout()),this,SLOT(getPoint()));
timer.start(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::getPoint()
{
if(i < realWidth)
{
point = QPoint(i,(uint(qrand())) % realHeight);
i++;
}
else
{
i = i % realWidth;
point = QPoint(i,(uint(qrand())) % realHeight);
i++;
}
emit haveData(point);
}
void MainWindow::getPointAndDraw(QPoint point)
{
index = point.x();
QPainter painter(&pixmap);
painter.setPen(Qt::green);
painter.drawLine(lastPoint,point);
painter.setPen(Qt::black);
painter.setBrush(Qt::red);
painter.drawRect(index+1,0,5,realHeight);
if(point.x() < realWidth-1)
lastPoint = point;
else
lastPoint = QPoint(0,0);
this->repaint(index-1,0,5,realHeight);
}
void MainWindow::paintEvent(QPaintEvent *e)
{
//return ;
QPainter painter(this);
QRect target1(0, 0, realWidth, realHeight/5);
QRect target2(0, realHeight/5, realWidth, realHeight/5);
QRect target3(0, 2*realHeight/5, realWidth, realHeight/5);
QRect target4(0, 3*realHeight/5, realWidth, realHeight/5);
QRect target5(0, 4*realHeight/5, realWidth, realHeight/5);
QRect source(0, 0, realWidth, realHeight);
painter.drawPixmap(target1,pixmap,source);
painter.drawPixmap(target2,pixmap,source);
painter.drawPixmap(target3,pixmap,source);
painter.drawPixmap(target4,pixmap,source);
painter.drawPixmap(target5,pixmap,source);
}
void MainWindow::resizeEvent(QResizeEvent *e)
{
realWidth = this->width();
realHeight = this->height();
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
qt repaint 用法_关于Qt 中update()和repaint()的区别相关推荐
- qt repaint 用法_Qt重绘之update,repaint详解
Qt里面的重绘和Windows编程里面的重绘差不多.但是Qt的重绘更有特色,更加智能. 在讲之前,先说说paintEvent() paintEvent()是一个虚函数槽(slot),子类可以对父类的p ...
- Qt中update()和repaint()的区别
转载:https://blog.csdn.net/qq_35396127/article/details/77745204 解释1 voidQWidget::repaint ( int x, int ...
- qt repaint 用法_Qt重绘之update,repaint详解(转)
Qt里面的重绘和Windows编程里面的重绘差不多.但是Qt的重绘更有特色,更加智能. 在讲之前,先说说paintEvent() paintEvent()是一个虚函数槽(slot),子类可以对父类的p ...
- mac os qt项目编译_【Qt开发】第一个Qt程序Hello World!
一:说在前头 我的第一份工作是做生产工具,当时用的MFC,IDE是VC6.0,现在想想真是古董级别,10年至今,微软也一直没有对MFC进行升级,冥冥中感觉微软自己都放弃MFC了,市场上貌似MFC的岗位 ...
- esc键 qt 退出菜单_【Qt】QDialog之屏蔽Esc键【转】
简述 Qt中Esc键会在一些控件中默认的进行一些事件的触发,比如:QDialog,按下Esc键窗口消失.大多数情况下,我们不需要这么做,那么就需要对默认事件进行屏蔽. 源码分析 通过查看QDialog ...
- vue方法调用失败后多次调用_浅析Vue中 computed / watch / methods的区别
思考:请说下Vue中computed 和 watch 的区别( 面试题 ) 构造选项 computed / watch / methods computed ● computed 起初构想 在Vue的 ...
- uniapp 获取到js文件var一个变量怎么获取到这个变量值_浅析Js中const,let,var的区别及作用域...
理解:let变量的作用域只能在当前函数中 js中const,let,var的区别及作用域_lianzhang861的博客-CSDN博客blog.csdn.net 全局作用域中,用 const 和 l ...
- qt repaint 用法_Qt 中update()和repaint()的区别
void QWidget::repaint ( int x, int y, int w, int h, bool erase = TRUE ) [槽] 通过立即调用paintEvent()来直接重新绘 ...
- python3 isinstance用法_对python中assert、isinstance的用法详解
1. assert 函数说明: Assert statements are a convenient way to insert debugging assertions into a program ...
最新文章
- http连接过程遇到的各种性能瓶颈
- 设置oracle服务自动启动
- MySQL工具1:mysqladmin
- 为什么下载源码包需要到官网上去下载?
- (一)导学(Javascript设计模式系统讲解与应用)
- 【招聘(上海)】东方财富证券招聘.net开发
- 【HTML5】Server-Sent服务器发送事件
- mybatis更新时间字段_你以为把Mybatis型处理器了解了,就不会“暴雷”了!
- 软件工程学习进度06
- 赚钱的逻辑和赚钱的道具要发生改变
- 洛谷——P1739 表达式括号匹配
- Windows8 Metro应用开发之C#(3)- 数据控件ListView、GridView、FlipView
- java 11下载_jdk11版
- Linux系统中的用户管理
- RTL8211E网口芯片在Uboot下调试问题
- 教你用Java获取IP归属地
- 设置导航标题颜色以及导航条背景色
- Html5 视频播放之video标签的使用(包含注意事项,例如视频无法正常展示,只显示音频等问题)
- 青少年沉迷游戏得鱼教育老师告诉家长如何处理
- php得到明天凌晨,PHP获得今天凌晨时间戳,明天凌晨时间戳,获取凌晨时间戳