Solved QTimer not calling slot
-
@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)