方法showGPS()
,应该是MyWidget
类的一个槽。然后,它只是使用QTimer
类的问题。
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), myWidget, SLOT(showGPS()));
timer->start(15000); //time specified in ms
上面的代码将每 15 秒调用一次 showGPS()。由于调用是周期性的,因此您不必使用该setSingleShot()
方法将计时器设置为一次性模式。
编辑:
这是一个简单的 poc,可以帮助您理解它..
#include <QApplication>
#include <QtGui>
#include <qobject.h>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget()
{
timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(showGPS()));
timer->start(1000); //time specified in ms
}
public slots:
void showGPS()
{
qDebug()<<Q_FUNC_INFO;
}
private:
QTimer *timer;
};
int main(int argc, char **args)
{
QApplication app(argc,args);
MyWidget myWidget;
return app.exec();
}