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. java和C#之间SOCKET通信的问题

    转自:http://www.cdtarena.com/javapx/201307/9170.html java和C#之间SOCKET通信的问题 一.服务器端(使用java编写) /** * 监听客户端 ...

  2. Android开发 第一篇

    关于android开发,new项目通知: 之前的new -> android project,现在更改为new -> android application project,同学们可以继续 ...

  3. jsp小商城

    一个小商城,当然,没淘宝那么厉害,只是那时学完j2ee后,发现java原来也可以做网站,学了数据库,servlet,jsp,当时是很惊喜的,可以直接做个这样的东西.而放到今天,学了更多之后,发现可以用 ...

  4. source code of MES Data

    <HTML> <HEAD> <TITLE>TELOGS</TITLE> </HEAD> <BODY> <?php /* c ...

  5. IPv6-only 的兼容性解决方案

    前几天Apple宣布 6月1日后所有应用必须支持IPv6-only网络 今天抽空看了下这方面的知识 首先解释下IPv6的是什么? 维基百科的定义如下:IPv6是Internet Protocol ve ...

  6. unity学习中经常要碰到的几种数据结构

    常碰到的几种数据结构:1.Array,2.ArrayList,3.List<T>,4.LinkedList<T>,5.Queue<T>,6.Stack<T&g ...

  7. PyQt中如何结合Qt设计师进行开发

    t设计师是Qt的所见即所得的界面设计工具,通过拖拉方式设计界面,但它并不能产生任何代码. Qt设计师使用.ui后缀的XML文件来存储界面内容.通过pyuic4命令可以编译成.py文件,.py文件的内容 ...

  8. Python 函数基础、有序集合、文件操作(三)

    一.set 特点: set是一个无序且不重复的元素集合访问速度快:天生解决元素重复问题 方法: 初始化 >>> s1 = set()>>> print(type(s ...

  9. 数据结构C语言版 有向图的十字链表存储表示和实现

    /*1wangxiaobo@163.com 数据结构C语言版 有向图的十字链表存储表示和实现 P165 编译环境:Dev-C++ 4.9.9.2 */ #include <stdio.h> ...

  10. Week6(10月14日)

    Part I:提问  =========================== 1.什么是视图模型?2.我们在留言本中,加入了一个怎样的视图模型?如何处理它? Part II:Ch05 视图模型 === ...