QT中的应用程序

编程入门 行业动态 更新时间:2024-10-22 19:41:31
本文介绍了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中的应用程序

本文发布于:2023-10-13 00:05:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   QT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!