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的区别的更多相关文章

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

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

  2. Qt 中update()和repaint()的区别

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

  3. Qt update刷新之源码分析(一)

    在做GUI开发时,要让控件刷新,会调用update函数:那么在调用了update函数后,Qt究竟基于什么原理.执行了什么代码使得屏幕上有变化?本文就带大家来探究探究其内部源码. Qt手册中关于QWid ...

  4. Unity3D中Update和Lateupdate的区别

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

  5. 【转】 Update和FixedUpdate的区别

    MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...

  6. QT、QTE、qtopia区别

    QT.QTE.qtopia区别 Qt的授权是分为两条线,商业版和开源版.如果使用商业版的Qt,那么开发出的程序可以是私有的和商业的:如果使用的是开源版的Qt,由于其使用的是GPL协议,那么可发出的程序 ...

  7. Unity3D中的Update, FixedUpdate, LateUpdate的区别

    MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...

  8. Qt 的两个许可证区别分析:LGPL 和商业协议

    Qt 的两个许可证区别分析:LGPL 和商业协议 Qt 有两个许可证:LGPL 和商业协议.这两个协议在现在的 Qt 版本中的代码是完全一致的(潜在含义是,Qt 的早期版本,商业版的 Qt 通常包含有 ...

  9. QT,QT/E,Qtopia,qt creator的联系与区别

    关于qt,qte,qtopia,qt creator它们之间的区别和联系,相信对所有刚刚入门qt的同学来说都是很模糊的.我在刚开始接触qt的时候也是这样,而且我第一次接触的是qte,因为要在arm上开 ...

随机推荐

  1. use utf8

    [root@wx03 0724]# cat a2.pl use Encode; my $a=<STDIN>; my $b=encode_utf8('微信'); print "\$ ...

  2. CMake学习小结

    假定有vegagis工程,工程的目录结构如下: #--vegagis#  |--src 源文件目录#     |--gui 界面工程,输出类型:dll,依赖于QT的QtCore.QtGui.QtXml ...

  3. IT第三天 - 数据类型、转换、Scanner使用

    IT第三天 上午 变量类型 1.6种数值类型:byte.short.int.long.float.double:其中byte是8个字节,short是16字节,int是32字节.long是64字节:日常 ...

  4. Java学习之二分查找算法

    好久没写算法了.只记得递归方法..结果测试下爆栈了. 思路就是取范围的中间点,判断是不是要找的值,是就输出,不是就与范围的两个临界值比较大小,不断更新临界值直到找到为止,给定的集合一定是有序的. 自己 ...

  5. Android创建启动画面

    每一个Android应用启动之后都会出现一个Splash启动界面,显示产品的LOGO.公司的LOGO或者开发人员信息.假设应用程序启动时间比較长,那么启动界面就是一个非常好的东西,能够让用户耐心等待这 ...

  6. 2013 成都网络赛 1004 Minimum palindrome

    题目大意:用m个字母组成一个长度为N的字符串,使得最长的回文子串 的长度最小. 并且要求字典序最小. 思路:分类模拟. 当M为1 的时候就直接输出N个A 当M大于2的时候就循环ABC 当M等于2的时候 ...

  7. android -- 蓝牙 bluetooth (三)搜索蓝牙

    接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...

  8. 同时支持多家云平台的管理工具HybridFox

    偶然间发现了这个firefox上的开元插件 号称支持AWS,Eucalyptus,OpenStack,OpenNebula 目的是通过一个入口实现异种云平台的管理 主要功能包括: Manage Ima ...

  9. PHP学习笔记2-流程控制

    条件控制:if <?php function getLevel($score){ if($score>=90){ return "优秀"; }elseif($score ...

  10. 树莓派deian的linux常用命令

    Linux系统,这个强大的系统,现在树莓派也要用到.给大家普及一下. 那些常用的Linux命令 linux的文件结构 /   根目录下的目录 /bin /home /dev /usr /opt /et ...