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<QPainter>
#include<QDebug>
#include<QMessageBox>
#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<QPainter>
#include<QDebug>
#include<QMessageBox>
#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<QPainter>
#include<QDebug>
#include<QMessageBox>
#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;
}
}
http://blog.csdn.net/songjinshi/article/details/6569910
QT update和repaint的区别的更多相关文章
- Qt常用函数 记录(update erase repaint 的区别)
一界面重载函数使用方法:1在头文件里定义函数protected: void paintEvent(QPaintEvent *event); 2 在CPP内直接重载void ----------::pa ...
- Qt 中update()和repaint()的区别
void QWidget::repaint ( int x, int y, int w, int h, bool erase = TRUE ) [槽]通过立即调用paintEvent()来直接重新绘制 ...
- Qt update刷新之源码分析(一)
在做GUI开发时,要让控件刷新,会调用update函数:那么在调用了update函数后,Qt究竟基于什么原理.执行了什么代码使得屏幕上有变化?本文就带大家来探究探究其内部源码. Qt手册中关于QWid ...
- Unity3D中Update和Lateupdate的区别
Unity中Update和Lateupdate的区别.Lateupdate和Update每一祯都被执行,但是执行顺序不一样,先执行Updatee然后执行lateUpdate. 如果你有两个脚本JS1. ...
- 【转】 Update和FixedUpdate的区别
MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...
- QT、QTE、qtopia区别
QT.QTE.qtopia区别 Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序 ...
- Unity3D中的Update, FixedUpdate, LateUpdate的区别
MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...
- Qt 的两个许可证区别分析:LGPL 和商业协议
Qt 的两个许可证区别分析:LGPL 和商业协议 Qt 有两个许可证:LGPL 和商业协议.这两个协议在现在的 Qt 版本中的代码是完全一致的(潜在含义是,Qt 的早期版本,商业版的 Qt 通常包含有 ...
- QT,QT/E,Qtopia,qt creator的联系与区别
关于qt,qte,qtopia,qt creator它们之间的区别和联系,相信对所有刚刚入门qt的同学来说都是很模糊的.我在刚开始接触qt的时候也是这样,而且我第一次接触的是qte,因为要在arm上开 ...
随机推荐
- spoj 3871 gcd extreme
题目大意给出一个n,求sum(gcd(i,j),<i<j<=n); 可以明显的看出来s[n]=s[n-]+f[n]; f[n]=sum(gcd(i,n),<i<n); 现 ...
- QT https post请求(QNetworkRequest要设置SSL证书,而SSL证书认证有三种,实测成功)
以VS开发为例.因为https访问需要用到SSL认证,而QT默认是不支持SSL认证,所以在使用之前必须先做一些准备工作: 需要安装OpenSSL库: 1.首先打开http://slproweb.com ...
- AOP(转)
AOP是什么?AOP(Aspect-Oriented Programming),面向切面编程,看着是跟OOP(面向对象编程)挺相近的,但实际上又有什么区别呢?OOP具有封装,继承,多态等东西来定义从上 ...
- 用PHP编写Hadoop的MapReduce程序
用PHP编写Hadoop的MapReduce程序 Hadoop流 虽然Hadoop是用Java写的,但是Hadoop提供了Hadoop流,Hadoop流提供一个API, 允许用户使用任何语言编 ...
- 基于visual Studio2013解决C语言竞赛题之前言
前言:关于VS2013 刚装完VS2013,新的IDE给人全新的编程体验,界面比以前更急简洁漂亮,不多说了,先上图吧 第一次启动VS2013
- C# L该系统的应用istView简单的图像浏览器
最近有同学问我怎么用ListView加载图像列表,在...前面"C#系统中的应用"于TreeView+ListView+ContextMenuStrip控件实现树状图显示磁盘文件夹 ...
- MFC下的日历表
// CalenderDlg.h : header file // #if !defined(AFX_CALENDERDLG_H__8DC8F113_2A47_45B8_8266_75CB406D68 ...
- ASP.NET站点安全
<configuration> <appSettings/> <connectionStrings> <add name="MyBookShop&q ...
- POJ1054 枚举【STL__binary_search()_的应用】
①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, ...
- Qt 获取字符串的UTF8编码值
看到群里有人在问怎么获取字符串的UTF8编码值 自己测试了下 熟悉下函数 <span style="font-size:18px;"> ui->setupU ...