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. win32多线程程序设计笔记(第四章下)

    上一笔记讲了同步机制中的临界区域(Critical Sections).互斥器(Mutexes),下面介绍同步机制中的另外两种. 信号量(Semaphores) 举个例子: 现在有人要租车,接待他的代 ...

  2. X-UA-Compatible IE 浏览器默认文档模式设置

    制作网页的时候,IE8浏览器浏览页面的时候,有时候文档模式默认是IE7,导致IE8兼容性不是非常好.出现IE7应该出现的模式. 解决的方法例如以下: 在X-UA-Compatible中可用的方法有: ...

  3. Winsock编程基础介绍 .

    相信很多人都对网络编程感兴趣,下面我们就来介绍,在网络编程中应用最广泛的编程接口Winsock API. 使用Winsock API的编程,应该了解一些TCP/IP的基础知识.虽然你可以直接使用Win ...

  4. 搞清楚学习Web的目的,是为了推广自己的产品和服务,不是为了替人接单做网页

    只有这样,Web才不会沉沦于下流,才会对自己的事业有真正的帮助-

  5. Android 修改底部导航栏navigationbar的颜色

    Android 修改底部导航栏navigationbar的颜色 getWindow().setNavigationBarColor(Color.BLUE); //写法一 getWindow().set ...

  6. 一种SpringMVC调用Javascript的方法

    在spring的controller里面,一般是进行业务逻辑的处理,组织数据模型,然后选择适当的显示模版进行展示,这种情况下,我们可以事先做一个js的模版,然后controller根据不同的参数调用此 ...

  7. redis(一)简介

    啥也不说百度各种简介 推荐一个博客灰常详细的介绍  nosqlfan  接下来发布redis+USE_TCMALLOC 的安装配置,博客好多坑,找一篇好文真心难啊.  redis+keepalived ...

  8. mysql 创建临时节点

    use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO::Socket; my $zk = ZooKeeper->new(ho ...

  9. 基于visual Studio2013解决C语言竞赛题之0412水仙花数

       题目 解决代码及点评 按照题目要求,3位数是从100~999,那么我们设计一个for循环遍历所有三位数 对每个三位数进行水仙花数的判断即可 /******************** ...

  10. Linux高性能server编程——高级I/O函数

     高级I/O函数 pipe函数 pipe函数用于创建一个管道,实现进程间的通信. #include <unistd.h> int pipe(int pipefd[2]); 通过pipe ...