2014-01-15 71 views
0

我查看了文檔,但沒有發現有關重複使用唯一標識的內容。QTimer是否重複使用唯一ID?

的文件說,有關startTimer所功能:

The function returns a unique integer timer ID 

但要多久將是獨一無二的?它在某些時候重用了ID嗎?

+0

我能跟蹤到qabstracteventdispatcher一路。用於Qt 5.2的src/qtbase/src/corelib/kernel下的cpp/.h。該文件的頂部設置參數,然後你可以看看QAbstractEventDispatcherPrivate :: allocateTimerId()。 – Huy

+0

根據[文檔](http://qt-project.org/doc/qt-4.8/qtimer.html#alternatives-to-qtimer),可以創建與定時器數量相等的定時器數量操作系統能夠提供。 –

回答

0

但它會獨特多久?

計時器ID應該保持唯一,直到它通過QAbstractEventDispatcherPrivate :: releaseTimerId()被釋放。

換句話說,調用killTimer()。

它在某個點重複使用ID嗎?

我很快就寫了一個簡單的測試,看看是否計時器ID會得到重用:

Something.h:

#include <QCoreApplication> 
#include <QtCore> 
#include <QtGlobal> 

class Something : public QObject 
{ 
    Q_OBJECT 

public: 
    Something() : QObject() {} 
    ~Something() {} 

}; 

main.cpp中:

#include "Something.h" 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 

    Something thing; 

    int i = thing.startTimer(1000); 
    qDebug() << i; 

    thing.killTimer(i); 

    i = thing.startTimer(1000); 
    qDebug() << i; 

    return a.exec(); 
} 

結果:

1 
1 

最後,使用QTimer獨家......你可以測試:

class Something : public QObject 
{ 
    Q_OBJECT 

public: 
    Something() : QObject(), timer(NULL) {} 
    ~Something() {} 

    void runme() 
    { 

    timer = new QTimer(); 
    // Old school ;) 
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent())); 
    timer->start(100); 
    } 

public Q_SLOTS: 

    void timerEvent() 
    { 
    qDebug() << timer->timerId(); 
    timer->stop(); 

    delete timer; 
    timer = new QTimer(); 
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent())); 
    timer->start(1000); 

    } 

public: 
    QTimer* timer; 
}; 

然後,只需啓動它並運行它:

Something thing; 
    thing.runme(); 
+1

而不是直接刪除'timer-> deleteLater()','QTimer :: timeout()'信號仍在執行。 – RobbieE

+0

當我像你一樣(開始/殺死/開始)時,我得到了ID 654311482和687865914。 – Vincent

相關問題