本文介绍了QT中的应用程序->处理消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Borland 6中,我经常用它来取消程序动作:

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

Application->Processmessages();

现在,使用QT 4.8.1,我在这个外国人中找不到(对我来说)

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

有人可以帮我吗?

推荐答案

在Qt,您将使用静态函数 QApplication :: processEvents()

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

A,您的问题是您的代码设计已损坏。您永远不必只为了解开事情而调用 processEvents 。您的所有GUI代码都应由运行至完成的方法组成,这些方法需要很短的时间(以毫秒为单位:〜0.001s)。如果需要花费更长的时间,则必须将其分成较小的部分,并在处理完每个部分后将控制权返回到事件循环。

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.

下面是一个示例:

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.

如果您要调用第三个一方阻止库代码,那么唯一(不幸的)解决方法是将这些操作放入QObject的插槽中,然后将该QObject移至工作线程。

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.

这篇关于QT中的应用程序->处理消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:39