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 = ;
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();
}
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+,,,realHeight);
if(point.x() < realWidth-)
lastPoint = point;
else
lastPoint = QPoint(,);
update();
// this->repaint(index-1,0,5,realHeight);
}
void MainWindow::paintEvent(QPaintEvent *e)
{
//return ;
QPainter painter(this);
QRect target1(, , realWidth, realHeight/);
QRect target2(, realHeight/, realWidth, realHeight/);
QRect target3(, *realHeight/, realWidth, realHeight/);
QRect target4(, *realHeight/, realWidth, realHeight/);
QRect target5(, *realHeight/, realWidth, realHeight/);
QRect source(, , 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 = ;
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();
}
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+,,,realHeight);
if(point.x() < realWidth-)
lastPoint = point;
else
lastPoint = QPoint(,);
update();
// this->repaint(index-1,0,5,realHeight);
}
void MainWindow::paintEvent(QPaintEvent *e)
{
//return ;
QPainter painter(this);
QRect target1(, , realWidth, realHeight/);
QRect target2(, realHeight/, realWidth, realHeight/);
QRect target3(, *realHeight/, realWidth, realHeight/);
QRect target4(, *realHeight/, realWidth, realHeight/);
QRect target5(, *realHeight/, realWidth, realHeight/);
QRect source(, , 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;
}
}
()继续改进(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 = ;
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();
}
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+,,,realHeight);
if(point.x() < realWidth-)
lastPoint = point;
else
lastPoint = QPoint(,);
this->repaint(index-,,,realHeight);
}
void MainWindow::paintEvent(QPaintEvent *e)
{
//return ;
QPainter painter(this);
QRect target1(, , realWidth, realHeight/);
QRect target2(, realHeight/, realWidth, realHeight/);
QRect target3(, *realHeight/, realWidth, realHeight/);
QRect target4(, *realHeight/, realWidth, realHeight/);
QRect target5(, *realHeight/, realWidth, realHeight/);
QRect source(, , 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://www.cnblogs.com/SkylineSoft/articles/2046303.html

http://www.informit.com/articles/article.aspx?p=1405227&seqNum=4

Qt 中update()和repaint()的区别的更多相关文章

  1. Unity3D中Update和Lateupdate的区别

    Unity中Update和Lateupdate的区别.Lateupdate和Update每一祯都被执行,但是执行顺序不一样,先执行Updatee然后执行lateUpdate. 如果你有两个脚本JS1. ...

  2. Qt常用函数 记录(update erase repaint 的区别)

    一界面重载函数使用方法:1在头文件里定义函数protected: void paintEvent(QPaintEvent *event); 2 在CPP内直接重载void ----------::pa ...

  3. Unity3D中Update()与FixedUpdate()的区别

    Unity3D中Update()与FixedUpdate()的区别是什么呢?从字面上理解,它们都是在更新时会被调用,并且会循环的调用.但是Update会在每次渲染新的一帧时,被调用.而FixedUpd ...

  4. Cocos2d中update与fixedUpdate的区别(六)

    它如何工作呢? update:和fixedUpdate:方法实际这样工作. Cocos2D将从iOS接口中取得时间间隔(delta)在你的游戏代码执行期间,并且检查fixedUpdate:方法在间隔期 ...

  5. Cocos2d中update与fixedUpdate的区别(五)

    在真实情况中update:和fixedUpdate方法如何去调用? 由上所述,所以update方法在每帧被调用1次,从而给你一个机会去更新你的游戏对象的状态在其绘制之前.而fixedUpdate:方法 ...

  6. QT update和repaint的区别

    void QWidget::repaint ( int x, int y, int w, int h, bool erase = TRUE ) [槽] 通过立即调用paintEvent()来直接重新绘 ...

  7. Cocos2d中update与fixedUpdate的区别(四)

    关于fixedUpdate:方法的目的 现在,想象一下在小球飞行的位置1到8之间有一个移动的平台: 该平台不停地上升和下降.有些时候小球可以不碰到而飘过平台,有些时候小球会和平台发生碰撞: 这表示小球 ...

  8. Cocos2d中update与fixedUpdate的区别(三)

    没错!现在的情况是很糟糕.因为玩家不会看到平滑的动作. 不管怎样,我们都对此无能为力.玩家期待在1秒后小球出现在位置(8),所以我们应该把球放在那里. 我们不会讨论如何避免掉帧的情况.对于这个例子我们 ...

  9. Cocos2d中update与fixedUpdate的区别(二)

    关于update:方法的目的 update:方法的目的在于给你一个更新你的游戏(你游戏中的所有对象,标签等待)的机会,在它们被渲染到屏幕之前. 换句话说,如果你想要一些游戏对象显示在屏幕的特定位置,你 ...

随机推荐

  1. myeclipse2014新感悟

    部署有两种方式:1.直接把文件拷贝到 tomcat下的webroot文件夹下 2.myeclipse软件内部点击“deploy”部署 →点击add→tomcat下的webroot文件夹下 点击完“运行 ...

  2. Linux文件查找命令find用法整理(locate/find)

    Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...

  3. get方式请求会出现中文乱码。post方式不会。

    get方式请求会出现中文乱码.post方式不会.   如果是要解决get方式中文乱码问题,就需要做一个拦截器,或者在web.xml做一个get请求的配置 来自为知笔记(Wiz)

  4. 深入理解Javascript变量作用域

    在学习JavaScript的变量作用域之前,我们应当明确几点: a.JavaScript的变量作用域是基于其特有的作用域链的. b.JavaScript没有块级作用域. c.函数中声明的变量在整个函数 ...

  5. (转)Java爬虫,信息抓取的实现

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...

  6. webrtc学习———记录三:mediaStreamTrack

    参考: https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack 转自http://c.tieba.baidu.com/p/3 ...

  7. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\d29b5393\123c3a1c\App_Code.odl3w4o6.dll”--“拒绝访问。 ”

    IIS部署网站或者Webservice时,出现以下问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...

  8. (转)网站隔几天打不开,多次重启Apache解决办法

    网站打不开了重启一下apache又好了,但过二天又打不开了,只有重启一下才好,基本上二天重起一次,本文主要解决这个问题 工具/原料 linux服务器 网站 方法/步骤   注意观察cpu占用情况{:s ...

  9. javascript类继承系列三(对象伪装)

    原理:在子类的构造器上调用超类构造器(父类构造器中的this指向子类实例),js提供了apply()和call()函数,可以实现这种调用 function baseClass() { this.col ...

  10. Quartz-2D绘图之路径(Paths)详解

    在上篇文章中,我们简单的理解了绘图上下文,今天我们来认识一下Quartz-2D中另一个重要的概念,路径(Paths). 一.理解路径 路径定义了一个或多个形状,或是子路径.一个子路径可由直线,曲线,或 ...