2014-01-15 73 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

相关问题