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. [LeetCode]题解(python):062-Unique Paths

    题目来源: https://leetcode.com/problems/unique-paths/ 题意分析: 给定两个整型m,n.判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有 ...

  2. github每次push都需要密码以及用户名的解决办法

    git remote set-url origin git@github.com:你的账户/项目名称.git就可以直接git push origin master了.

  3. [Android]Dalvik的BOOTCLASSPATH和dexopt流程

    BOOTCLASSPATH简介1.BOOTCLASSPATH是Android Linux的一个环境变量,可以在adb shell下用$BOOTCLASSPATH看到.2.BOOTCLASSPATH于/ ...

  4. QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)

    原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...

  5. boost::thread用法

    最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...

  6. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  7. 2013 ACM/ICPC Asia Regional Chengdu Online---1003

    哈哈哈 #include <iostream> #include <cstring> #include <string> #include <cstdio&g ...

  8. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  9. jquery 单击li防止重复加载的实现代码

    因为加载内容比较慢,所以用户可能在li上不经意点击了两次,那么就会请求两次,这是我们不想看到的. 今天在javascript-jquery群上一筒子发了两个demo给我,他的方法是先将单击的li节点拷 ...

  10. python 拼写检查代码(怎样写一个拼写检查器)

    原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...