如何在没有 QT 应用程序的情况下使用 QTimer

Sco*_*ion 0 c++ qt timer

我想在我的常规课程中使用 QTimer(没有 QT 应用程序,而是从 QTimer 派生)。但是当我尝试这个时:

标题:

#include <qtimer.h>
QTimer* m_timer;
public slots:
    void UpdateClock();
Run Code Online (Sandbox Code Playgroud)

来源:

    m_timer = new QTimer(this);
    QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(UpdateClock()));
    m_timer->start(1000);

void MyClass::UpdateClock()
{
    int i = 0;
}
Run Code Online (Sandbox Code Playgroud)

计时器永远不会跳转到 UpdateClock 方法!你知道为什么以及我如何解决这个问题吗?

谢谢!

arn*_*rnt 5

QTimer 依赖于 QCoreApplication。如果您不启动 QCoreApplication,则不会激活 QTimer。(QApplication继承QCoreApplication,通常使用。)