QTimer 的使用


QTimer(重復和單發計時器)

應用 QTimer 時,先創建一個 QTimer 類,利用 connect 將 timeout() 與對應槽函數連接,在調用 start() 函數設置定時器時間間隔,每經過設置時間后,定時器會發出一個 timeout(),

相應的槽函數就會被觸發,直到調用 stop() 函數停止。
舉例:
  QTimer *timer = new QTimer(this);
  connect(timer,SIGNAL(timeout()),this,SLOT(function));
  timer->start(1000);

  也可以不用定義QTimer類,直接調用QTimer的成員函數singleShot(),定時器只執行一次
  QTimer::singleShot(1000,this,SLOT(updateCaption())); // 1秒后啟動功能函數

成員函數

1)void QTimer::singleShot(int msec,Qt::TimerType timeType,const QObject *receiver,const *member) //在規定的時間間隔調用函數
舉例:
  #include
  #include

  int main(int argc,char *argv[])
  {
    QApplication app(argc,argv);
    QTimer::singleShot(10000,&app,SLOT(quit()));
    ........
    return app.exec();
  } //功能表述,在10秒鍾后,應用程序將關閉

2)void QTimer::start(int msec);
  啟動或者重啟服務器,msec 為時間間隔,沒有參數時,時間間隔為 0

3)void QTimer::stop();
  停止計時器

4)void QTimer::timeout();
  當定時器時間到時,信號被發送

5)int QTimer::timerID()
  返回正在運行的計時器的 ID 號,否則返回為 -1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM