How to call a function after every 15 seconds using QT

前端 未结 2 776
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 07:25

I know my question is similar to this QUESTION but i cant find solution from there. Can anyone give a breif answer to my problem?

I have a function like this

相关标签:
2条回答
  • 2020-12-15 08:05

    While you can use QTimer for checking file changes, in your case QFileWatcher might be better solution.

    0 讨论(0)
  • 2020-12-15 08:13

    The method showGPS(), should be made a slot of MyWidget class. Then on, its just a matter of using the QTimer class.

     QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), myWidget, SLOT(showGPS()));
        timer->start(15000); //time specified in ms
    

    The above code will call showGPS(), every 15 seconds. Since the call is periodic, you don't have to set the timer in one shot mode using the setSingleShot() method.

    Edit:

    This is a simple poc, to help you understand it..

    #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();
    }
    
    0 讨论(0)
提交回复
热议问题