简体   繁体   English

Qt-QTimer :: timeout()信号会导致QEvent吗?

[英]Qt - does a QTimer::timeout() signal result in a QEvent?

Qt doc: Qt doc:

If no event loop is running, events won't be delivered to the object. 如果没有运行任何事件循环,则事件将不会传递到该对象。 For example, if you create a QTimer object in a thread but never call exec(), the QTimer will never emit its timeout() signal. 例如,如果您在线程中创建QTimer对象,但是从不调用exec(),则QTimer将永远不会发出其timeout()信号。 Calling deleteLater() won't work either. 调用deleteLater()也不起作用。 (These restrictions apply to the main thread as well.) (这些限制也适用于主线程。)

Does this mean that signal void QTimer::timeout() will also issue a QEvent ? 这是否意味着信号void QTimer::timeout()也将发出QEvent
If so, where does the Qt doc state this? 如果是这样,那么Qt文档在哪里说明呢?

where does the Qt doc state this? Qt doc在哪里说明呢?

Nowhere, because it shouldn't matter to the user of QTimer . 无处,因为它对QTimer的用户无关紧要。 The timer event is an implementation detail. timer事件是实现细节。 It is delivered to the timer object itself, so you'd really have to go out of your way to intercept it. 它是传递给计时器对象本身的,因此您确实必须竭尽全力来拦截它。 Here's how QTimer works: QTimer工作方式如下:

class QTimer : public QObject {
  Q_TIMER
  QBasicTimer m_timer;
protected:
  void timerEvent(QTimerEvent * ev) override {
    if (ev->timerId() == m_timer.timerId())
      emit timeout();
  }
  /*...*/
};

If you think about it, there's no way of emitting any signals without running the code that emits the signals, and the only way to safely run such code that emits things asynchronously is to code for run-to-completion chunks that cede control to the event loop at every opportunity. 如果您考虑一下,在不运行发出信号的代码的情况下就无法发出任何信号,并且安全地运行此类异步发出代码的唯一方法是为完成控制的代码块编写代码,这些代码将控制权交给了每个机会都有事件循环。 The event loop is notified by the platform that a timer has timed out, and emits a signal right then. 平台会通知事件循环定时器超时,然后立即发出信号。 You'd be in deep trouble if Qt issued signals such as timer timeouts from intrusive asynchronous callbacks like Unix signals: just read about how few things you can do while in a signal handler - it'd be no different than an interrupt handler. 如果Qt从诸如Unix信号之类的侵入性异步回调发出诸如定时器超时之类的信号,您将陷入深深的麻烦:只需了解一下在信号处理程序中可以执行的操作-它与中断处理程序没有什么不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM