A mini sample file main.cpp
:
#include <iostream>
#include <QCoreApplication>
class MyApp : public QCoreApplication
{
private:
int m_idtimer;
public:
MyApp(int nargs, char* argc[]) : QCoreApplication(nargs, argc)
{
m_idtimer = startTimer(3000); // 3 segs.
}
protected:
void timerEvent(QTimerEvent* e)
{
char c = '\0';
std::cout << "timerEvent" << std::endl;
std::cin >> c;
if (c == 'q') {
killTimer(m_idtimer);
quit();
}
}
};
int main(int nargs, char* argc[])
{
MyApp app(nargs, argc);
return app.exec();
}
Extra mini Makefile
:
LDFLAGS = -I/usr/include/qt4 -I/usr/include/qt4/QtCore
LDLIBS = -lQtCore
Compiling and execution:
$ make main
g++ -I/usr/include/qt4/QtCore main.cpp -lQtCore -o main
$ ./main
timerEvent
1
timerEvent
2
timerEvent
3
timerEvent
q
$
Ok and then, my question. I’ve made this sample with the purpose of testing if timer events are accumulative.
When I executing the main
program, the next occurs:
- the first
timerEvent
message is shown after 3 seconds, andtimerEvent()
waits a character. - I press
1
inmediatly. - 3 seconds later, the second
timerEvent
message appear (as expected). - I wait some seconds (15 or more) and I press
2
- The third message is shown immediatly (one timer event accumulated).
- I press
3
immediatly. - And 3 seconds later the fourth message appear (no more timer events accumulated).
- I press
q
and the program ends.
Question: Why aren there no more timer events accumulated? Does this behaviour depend on the platform?
PD: My Qt version is 4.8, my SO Ubuntu 13.04, and my kernel (linux) 3.8.0-19-generic. The running graphic system is Gnome 3.
Your fifteen second wait will not accumulate timer events because your
timerEvent
code blocks waiting for the input. Qt can't get back into its event loop until you enter the input. When it does get back to the event loop it checks the elapsed time and notices that more than 3 seconds have elapsed and so it fires off a timer event. The fact that fifteen seconds has elapsed is irrelevant.This is the expected behaviour and will not (should not) be platform-dependent.