如何让Qt 的程序等待一段时间(等待的同时,还让主界面刷新图片)good
后面这种方法可以不影响其他线程的响应,又可以达到等待的目的。
测试的一个小例子:
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- QLabel *label;
- };
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- {
- label = new QLabel(this);
- label->resize(800,480);
- label->show();
- }
- Widget::~Widget()
- {
- delete label;
- }
- #include <QPixmap>
- #include <QTimer>
- #include <QDebug>
- #include <QThread>
- #include <QEventLoop>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- static const char *pic[4] = { //这个数组的用法很巧妙吧
- ":/new/prefix1/pic/Bottom Panel v1.3-compare-01.png",
- ":/new/prefix1/pic/Bottom Panel v1.3-compare-02.png",
- ":/new/prefix1/pic/Bottom Panel v1.3-compare-03.png",
- ":/new/prefix1/pic/Bottom Panel v1.3-compare-04.png",
- };
- while(1)
- {
- for(int i = 0; i < 4; i ++)
- {
- w.label->setPixmap(QPixmap(pic[i]));
- qDebug()<<"i:"<<i;
- //QThread::sleep(2); // 这样写label上的图片就无法正常刷新,换用下面的的eventloop就OK了
- QEventLoop eventloop;
- QTimer::singleShot(2000, &eventloop, SLOT(quit())); //wait 2s
- eventloop.exec();
- }
- }
- return a.exec();
- }
http://blog.csdn.net/zzwdkxx/article/details/47280975