QTimer Pointer Out of Scope?
Solved
General and Desktop
-
My slots seem to have a NULL pointer for myProgressTimer for the following code. I.e., When myProgressDialog exceeds the maximum value, or it is canceled, "myProgressTimer is NULL" is reported. However, the timer is still running. Any ideas?
class MyGUI : public QMainWindow { ... private slots: ... void updateProgress(); void cancelProgress(); ... private: ... QProgressDialog* myProgressDialog; QTimer* myProgressTimer; ... } MyGUI::MyGUI(QWidget *parent) : QMainWindow(parent) ... ,myProgressDialog(0) ,myProgressTimer(0) { ... void MyGUI::startSystem() { ... myProgressDialog = new QProcessDialog("Starting system...", "Cancel", 0, 20); QTimer *myProgressTimer = new QTimer(this); connect(myProgressDialog, SIGNAL(canceled()), this, SLOT(cancelProgress())); connect(myProgressTimer, SIGNAL(timeout()), this, SLOT(updateProgress())); myProgressTimer->start(1000); } ... void MyGUI::updateProgress() { static int steps = 0; myProgressDialog->setValue(steps++); if (setps > myProgressDialog->maximum()) { myProgressDialog->cancel(); if (myProgressTimer ) { myProgressTimer->stop(); } else { cout << "myProgressTimer is NULL" << endl; } } } void MyGUI::cancelProgress() { myProgressDialog->cancel(); if (myProgressTimer ) { myProgressTimer->stop(); } else { cout << "myProgressTimer is NULL" << endl; } }
-
@DougyDrumz
Hello,QTimer *myProgressTimer = new QTimer(this);
This line is in your constructor is bogus. You're hiding the global scope and are assigning the object's address to a local pointer variable.
Kind regards.
-
Duh! Good catch!
-
@DougyDrumz
Easy peasy ... no one seems to want to answer my questions though. ;)
Happy coding!