Please help me spot my ( C++) code error
-
The attached code supposedly "times out" .
Immediately since "timeout" is set by default to "'0" .
Setting
t->start(0) ;
gives
emainingTime( ); as zerobut
&QTimer::timeout SIGNAL
is never received
QTimer *t = new QTimer(); QProgressDialog *pd = new QProgressDialog("START Operation in progress.", "Cancel", 0, 100); connect(pd, &QProgressDialog::canceled, this, &Operation::cancel); t = new QTimer(this); connect(t, &QTimer::timeout, this, &Operation::perform); qDebug() << "TRACE Start timer t->start(0);"<< steps;; t->start(); qDebug() << "TRACE remaining time " << t->remainingTime(); qDebug() << "TRACE END Constructor Operation::Operation(QObject *parent) "<< steps;;
-
@AnneRanch said in Please help me spot my ( C++) code error:
QTimer *t = new QTimer();
...
t = new QTimer(this);
Why do you create the object twice?
-
@AnneRanch said in Please help me spot my ( C++) code error:
but
&QTimer::timeout SIGNAL
is never receivedIt really should be. Assuming you do not destroy either
t
orthis
(what is the lifetime/scope of yourOperation
instance? show where you create theOperation
instance) after your code, and you allow the Qt event loop to run, as soon as the event loop is next reachedthis->perform()
should be called. Note that it will not be called during the code you show, if that is what you were expecting. Even with0
timeout/time remaining it wioll happen the next time the event loop is reached. Please put aqDebug("Operation::perform()")
as the very first statement in yourvoid Operation::perform()
. -
Here is the latest.
Should have compiler complained about my double declaration of t?
I did test t.start(1000); and got t.remainingTime() = 950 so I would say there is no issue with timer anyway.As I said up front - I never get "Timeout " hence never get to "perform"
I do have "debug" in "perform " already. ( I do that most of the time when debugging )BUT there is something very wrong with my code - because the progress dialog have "cancel" button and that SIGNAL is also never processed by Operation::'cancel. .
Here is a full debug I get .
I get the progress dialog to show , but never runs because no :"timeout" SIGNAL.TEST DEBUG in main
DEBUG TRACE
File mainwindow.cpp
Function run_
@ line 271
START TEST case 5 Form *F = new Form();
TEST case 6 MainWindow_HCI_BACKGROUND *MHB = new MainWindow_HCI_BACKGROUND();
TRACE START Constructor Operation::Operation(QObject *parent) 0
TRACE Start timer t->start(0); 0
TRACE remaining time 0
TRACE END Constructor Operation::Operation(QObject *parent) 0Operation::Operation(QObject *parent) : QObject{parent},steps(0) { qDebug() << "TRACE START Constructor Operation::Operation(QObject *parent) "<< steps;; QTimer t; // = new QTimer(); QProgressDialog *pd = new QProgressDialog("START Operation in progress.", "Cancel", 0, 100); connect(pd, &QProgressDialog::canceled, this, &Operation::cancel); //t = new QTimer(this); connect(&t, &QTimer::timeout, this, &Operation::perform); qDebug() << "TRACE Start timer t->start(0);"<< steps;; t.start(); qDebug() << "TRACE remaining time " << t.remainingTime(); qDebug() << "TRACE END Constructor Operation::Operation(QObject *parent) "<< steps;; Operation::perform(); should this start "perform? it does not } void Operation::perform() { qDebug() << "TRACE void Operation::perform() timeout ?? steps "<< steps;; pd->setValue(steps); sleep(1); //... perform one percent of the operation steps++; if (steps > pd->maximum()) t->stop(); } void Operation::cancel() { qDebug() << "TRACE void Operation::cancel()"; t->stop(); //... cleanup }
-
@AnneRanch said in Please help me spot my ( C++) code error:
QTimer t; //
C++ basic question - how long does this object live?
-
@AnneRanch said in Please help me spot my ( C++) code error:
Should have compiler complained about my double declaration of t?
No.
QTimer * t = new QTimer(this); //< Declare t and initialize it t = new QTimer(this); //< Assign to t; no double declaration
Consider:
QTimer * const t = new QTimer(this); //< Declare t and initialize it, the pointer is immutable t = new QTimer(this); //< Compiler complains - sorry, no can do, the pointer is const