当前位置: 代码迷 >> QT开发 >> 完全搞不懂Qt了!该怎么解决
  详细解决方案

完全搞不懂Qt了!该怎么解决

热度:101   发布时间:2016-04-25 04:51:32.0
完全搞不懂Qt了!!!!
mainwindow.h
C/C++ code
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QPushButton>class MyWindow : public QMainWindow{    Q_OBJECTpublic:    MyWindow(QWidget *parent = 0);protected:    void paintEvent(QPaintEvent *);    void timerEvent(QTimerEvent *);private:   bool start;   QPixmap pixmap;};#endif // MAINWINDOW_H


mainwindow.cpp
C/C++ code
#include<QMainWindow>#include<QPainter>#include "mainwindow.h"MyWindow::MyWindow(QWidget *parent):QMainWindow(parent),pixmap(800,600){    start=true;    resize(800,600);    startTimer(5000);}void MyWindow::paintEvent(QPaintEvent *paintevent){    QPainter painter(this);    QRect rect(0,0,800,600);    QBrush brush(QColor(255,0,0));    painter.setBrush(brush);    if(start==true)      {          painter.drawRect(rect);          pixmap.fill(this,0,0); //保存第一次画的东西          start=false;      }   else      {          painter.drawPixmap(0,0,pixmap);//还原      }}void MyWindow::timerEvent(QTimerEvent *){    pixmap.fill(this,0,0);    update();}


main.cpp

C/C++ code
#include <QtGui/QApplication>#include "mainwindow.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    MyWindow w;    w.show();    return a.exec();}


这段程序窗口不是应该一直保持红色吗?
怎么我在ubuntu上窗口红色闪一下就变为了默认色,而在windows xp上却是红色维持的5s后涮新为默认色??
求解,纠结了好几天的问题了?










------解决方案--------------------
Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). The repaint() function does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so you are painting on the widget's background.

------解决方案--------------------
QT与MFC之类的库不同,它的paintEvent会经常被调用的(界面被重绘),一旦有重绘,你的start就变成false了。
建议先把需要画的界面画在QPixmap上,然后再显示在界面上。
  相关解决方案