QTimer not calling slot
-
And where are you calling initAll()?
-
Hi,
I'm very new to Qt, trying now my first program. Having trouble with QTimer which doesn't call the slot function. This is the relevant code (inside MainWindow):
int StatusTimerInterval = 2000; QTimer *statusTimer; . . void MainWindow::initAll(void) { . . statusTimer = new QTimer(); connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus)); statusTimer->setInterval(StatusTimerInterval); statusTimer->start(); . . } void MainWindow::updateStatus(void) { fprintf(stderr,"updateStatus\n"); }updateStatus never executes. I stepped through the the initializing code and made sure the timer is active. What am I missing here?
TIA
@ForeverNoob said in QTimer not calling slot:
statusTimer = new QTimer();
Just a side note without looking at your actual issue :)
Use
thisstatusTimer = new QTimer(this);to make sure your
QTimer(on heap) is included in your parent/child hierarchy and it gets cleaned up together with its parent (yourmainWindow). -
And where are you calling initAll()?
@Christian-Ehrlicher said in QTimer not calling slot:
And where are you calling initAll()?
In MainWindow::MainWindow.
-
@ForeverNoob said in QTimer not calling slot:
statusTimer = new QTimer();
Just a side note without looking at your actual issue :)
Use
thisstatusTimer = new QTimer(this);to make sure your
QTimer(on heap) is included in your parent/child hierarchy and it gets cleaned up together with its parent (yourmainWindow).connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
incorrect SLOT usage (missing parenthesis).
This is one of the reasons this old syntax is discouraged to be used.
Use the "new" syntax with function pointers instead. -
Hi,
I'm very new to Qt, trying now my first program. Having trouble with QTimer which doesn't call the slot function. This is the relevant code (inside MainWindow):
int StatusTimerInterval = 2000; QTimer *statusTimer; . . void MainWindow::initAll(void) { . . statusTimer = new QTimer(); connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus)); statusTimer->setInterval(StatusTimerInterval); statusTimer->start(); . . } void MainWindow::updateStatus(void) { fprintf(stderr,"updateStatus\n"); }updateStatus never executes. I stepped through the the initializing code and made sure the timer is active. What am I missing here?
TIA
@ForeverNoob said in QTimer not calling slot:
SLOT(updateStatus)Damn, just as @raven-worx has spotted! You would have to have written
SLOT(updateStatus())here. Which is why I said change over to new style syntax immediately, this is perfect reason, as @raven-worx says! :) -
connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
incorrect SLOT usage (missing parenthesis).
This is one of the reasons this old syntax is discouraged to be used.
Use the "new" syntax with function pointers instead.@raven-worx said in QTimer not calling slot:
incorrect SLOT usage (missing parenthesis).
cool, overseen this :)
-
connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
incorrect SLOT usage (missing parenthesis).
This is one of the reasons this old syntax is discouraged to be used.
Use the "new" syntax with function pointers instead.@raven-worx said in QTimer not calling slot:
connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
incorrect SLOT usage (missing parenthesis).
This is one of the reasons this old syntax is discouraged to be used.
Use the "new" syntax with function pointers instead.Hi,
I changed this statement to:
connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus()));But that didn't help, updateStatus still wasn't executing.
Then I tried the new syntax with something like this:
connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);This wouldn't compile, QT Creator resdponds with an error message "too few arguments to function call, expected 1, have 0". I'm not sure which argument is missing, prsumably in the timeout() function. Couldn't find any example anywhere.
-
Hi,
Remove the void from your slot argument list and try to build again.
-
@SGaist said in QTimer not calling slot:
Hi,
Remove the void from your slot argument list and try to build again.
OK, I forgot to add the function to the slot list. Everything works now. Thanks a lot to all who helped.
-
@raven-worx said in QTimer not calling slot:
connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus));
incorrect SLOT usage (missing parenthesis).
This is one of the reasons this old syntax is discouraged to be used.
Use the "new" syntax with function pointers instead.Hi,
I changed this statement to:
connect(statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus()));But that didn't help, updateStatus still wasn't executing.
Then I tried the new syntax with something like this:
connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);This wouldn't compile, QT Creator resdponds with an error message "too few arguments to function call, expected 1, have 0". I'm not sure which argument is missing, prsumably in the timeout() function. Couldn't find any example anywhere.
@ForeverNoob said in QTimer not calling slot:
connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);That should also be
connect(statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus); -
@ForeverNoob said in QTimer not calling slot:
connect(statusTimer, statusTimer->timeout(), this, &MainWindow::updateStatus);That should also be
connect(statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus);@JKSH said in QTimer not calling slot:
connect(statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus);
That works fine. Many thanks.
-
@SGaist said in QTimer not calling slot:
Remove the void from your slot argument list and try to build again.
@ForeverNoob
I had not noticed you had writtenvoid MainWindow::updateStatus(void). The same applies to yourvoid MainWindow::initAll(void). And presumably any other functions you have defined in a similar fashion. Unlike C, in C++, where declaring arguments to functions is mandatory, we don't writefunction(void)to indicate no parameters, we just writefunction(). -
@SGaist said in QTimer not calling slot:
Hi,
Remove the void from your slot argument list and try to build again.
OK, I forgot to add the function to the slot list. Everything works now. Thanks a lot to all who helped.
@ForeverNoob said in QTimer not calling slot:
OK, I forgot to add the function to the slot list. Everything works now. Thanks a lot to all who helped.
with the function pointer syntax the slots do not have to be declared as slots anymore.
Only if you want to call them via the meta-object system (but thats a different story)