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. Please help me spot my ( C++) code error
Qt 6.11 is out! See what's new in the release blog

Please help me spot my ( C++) code error

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.2k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    The attached code supposedly "times out" .
    Immediately since "timeout" is set by default to "'0" .
    Setting
    t->start(0) ;
    gives
    emainingTime( ); as zero

    but

    &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;;
    
    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • A Anonymous_Banned275

        The attached code supposedly "times out" .
        Immediately since "timeout" is set by default to "'0" .
        Setting
        t->start(0) ;
        gives
        emainingTime( ); as zero

        but

        &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;;
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @AnneRanch said in Please help me spot my ( C++) code error:

        but
        &QTimer::timeout SIGNAL
        is never received

        It really should be. Assuming you do not destroy either t or this (what is the lifetime/scope of your Operation instance? show where you create the Operation instance) after your code, and you allow the Qt event loop to run, as soon as the event loop is next reached this->perform() should be called. Note that it will not be called during the code you show, if that is what you were expecting. Even with 0 timeout/time remaining it wioll happen the next time the event loop is reached. Please put a qDebug("Operation::perform()") as the very first statement in your void Operation::perform().

        1 Reply Last reply
        1
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by
          #4

          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) 0

          Operation::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
          }
          
          kshegunovK 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @AnneRanch said in Please help me spot my ( C++) code error:

            QTimer t; //

            C++ basic question - how long does this object live?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • A Anonymous_Banned275

              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) 0

              Operation::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
              }
              
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @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
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2

              • Login

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