Application->Processmessages in QT?

淺唱寂寞╮ 提交于 2019-12-10 15:28:39

问题


In Borland 6 I often use this to unstuck program action:

Application->Processmessages();

Now, with QT 4.8.1, I don't have found in this foreign (for me) documentation of QT.

Can anyone help me?


回答1:


In Qt, you'd use the static function QApplication::processEvents().

Alas, your issue is that the design of your code is broken. You should never need to call processEvents simply to "unstuck" things. All of your GUI code should consist of run-to-completion methods that take a short time (on the order of single milliseconds: ~0.001s). If something takes longer, you must split it up into smaller sections and return control to the event loop after processing each section.

Here's an example:

class Worker: public QObject
{
  Q_OBJECT
  int longWorkCounter;
  QTimer workTimer;
public:
  Worker() : ... longWorkCounter(0) ... {
    connect(workTimer, SIGNAL(timeout()), SLOT(longWork());
  }
public slots:
  void startLongWork() {
    if (! longWorkCounter) {
      workTimer.start(0);
    }
  }
private slots:
  void longWork() {
    if (longWorkCounter++ < longWorkCount) {
      // do a piece of work
    } else {
      longWorkCounter = 0;
      workTimer.stop();
    }
  }
};

A zero-duration timer is one way of getting your code called each time the event queue is empty.

If you're calling third party blocking library code, then the only (unfortunate) fix is to put those operations into slots in a QObject, and move that QObject to a worker thread.



来源:https://stackoverflow.com/questions/11176488/application-processmessages-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!