我查看了文档,但没有找到关于重用唯一ID的内容。
文档说明了startTimer函数:
The function returns a unique integer timer ID
但它会有多长时间独特?是否在某些时候重用了这些ID?
答案 0 :(得分: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();