Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTimer Pointer Out of Scope?

QTimer Pointer Out of Scope?

Scheduled Pinned Locked Moved Solved General and Desktop
qt4qtimerqprogressdialog
4 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DougyDrumz
    wrote on 17 Feb 2016, 18:41 last edited by
    #1

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

    Dougy Drumz

    K 1 Reply Last reply 17 Feb 2016, 18:42
    0
    • D DougyDrumz
      17 Feb 2016, 18:41

      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;
          }        
       }
      
      
      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 17 Feb 2016, 18:42 last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • D Offline
        D Offline
        DougyDrumz
        wrote on 17 Feb 2016, 19:35 last edited by
        #3

        Duh! Good catch!

        Dougy Drumz

        K 1 Reply Last reply 17 Feb 2016, 19:58
        0
        • D DougyDrumz
          17 Feb 2016, 19:35

          Duh! Good catch!

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 17 Feb 2016, 19:58 last edited by kshegunov
          #4

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

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0

          1/4

          17 Feb 2016, 18:41

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved