我是 qt 开发的新手,面临一个问题。我想每 1 秒调用一个函数 updateCompass 来更新小部件中已经绘制的罗盘值。
我需要知道如何以及在哪里调用该函数。
pax*_*blo 10
Qt 有一个特定的方法可以做到这一点,即使用QTimer类.
它允许您创建一个计时器(一次性或周期性)并将其timeout信号连接到您需要编织魔法的任何插槽(功能)。
事实上,该链接页面恰好包含您执行所需功能所需的代码:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
Run Code Online (Sandbox Code Playgroud)
这将导致您的update函数每秒被调用一次(在 Qt re 准确性等施加的限制内)。
以你的情况为例。
QTimer *timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(updateCompass()));
timer->start(1000);
Run Code Online (Sandbox Code Playgroud)
这updateCompass()每秒都会调用。