方法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
#include
#include
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()<
}
private:
QTimer *timer;
};
int main(int argc, char **args)
{
QApplication app(argc,args);
MyWidget myWidget;
return app.exec();
}