QTimer Pointer Out of Scope?



  • 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;
        }        
     }
    
    

  • Qt Champions 2016

    @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!


  • Qt Champions 2016

    @DougyDrumz
    Easy peasy ... no one seems to want to answer my questions though. ;)
    Happy coding!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.