QTimer not calling slot
-
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
You're not missing anything, it should be working. Assuming you have set the event loop executing from yourmain()
, viaQApplication::exec()
, as most examples show. And you are not "blocking" the event loop from running in any code you write.In
updateStatus()
useqDebug() << "updateStatus"
instead of thefprintf()
, just in case. Output would come in the Application Output tab if you are in Qt Creator.Since you are starting out, ignore all the world's examples using
SIGNAL
/SLOT()
macros. Read https://wiki.qt.io/New_Signal_Slot_Syntax and change over to using new style syntax for all cases. -
@JonB said in QTimer not calling slot:
QApplication::exec()
Hi JonB, thanks for quick reply.
This is how my main() looks:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
Automatically generated by QR Creator. Anything to do here?
TIA
-
And where are you calling initAll()?
-
@ForeverNoob said in QTimer not calling slot:
statusTimer = new QTimer();
Just a side note without looking at your actual issue :)
Use
this
statusTimer = 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
). -
@Christian-Ehrlicher said in QTimer not calling slot:
And where are you calling initAll()?
In MainWindow::MainWindow.
-
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. -
@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! :) -
@raven-worx said in QTimer not calling slot:
incorrect SLOT usage (missing parenthesis).
cool, overseen this :)
-
@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.
-
@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()
. -
@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)