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 used to time adding to a QList for specified time?
Forum Updated to NodeBB v4.3 + New Features

QTimer used to time adding to a QList for specified time?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 824 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.
  • C Offline
    C Offline
    Calicoder
    wrote on last edited by
    #1

    Howdy QT'ers, wondering if anyone knows if it's possible to time when items are added to a QList using a QTimer? I've come up with this pseudocode so far: (slowly teaching myself C++ and QT coding thanks to you guys during my retirement years)

    QList<QString> holderList;
    int timeout = 500;
    QTimer timer;
    timer.start();
    
    if (timer.elapsed() != 0)
    {
          holderList.append("test-string");
    }
    

    I'm looking to limit the appending to the QList to 500 ms. Once that limit is hit, stop appending. Is this possible?

    Thanks for the help and any suggestions.

    KroMignonK J.HilkJ 2 Replies Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      Connect something (slot) that adds to the list to the QTimer timeout() signal and start() the timer with an interval (or set the interval and start the timer separately). If you want to stop after 500 milliseconds then also check the number of times the slot has been called, then stop the QTimer.
      https://doc.qt.io/qt-5/qtimer.html#details

      C 1 Reply Last reply
      2
      • C ChrisW67

        Connect something (slot) that adds to the list to the QTimer timeout() signal and start() the timer with an interval (or set the interval and start the timer separately). If you want to stop after 500 milliseconds then also check the number of times the slot has been called, then stop the QTimer.
        https://doc.qt.io/qt-5/qtimer.html#details

        C Offline
        C Offline
        Calicoder
        wrote on last edited by
        #3

        @ChrisW67

        Thanks for the quick reply. If I'm understanding this correctly, I would setup the connect code with the timeout signal triggering the append to list method without any QTimer code in it. To stop it from adding new data, I can check the number of times it was called. This is where I'm kinda lost: how many times is it called in say 500 ms? Is that set up in the connect code?

        1 Reply Last reply
        0
        • C Calicoder

          Howdy QT'ers, wondering if anyone knows if it's possible to time when items are added to a QList using a QTimer? I've come up with this pseudocode so far: (slowly teaching myself C++ and QT coding thanks to you guys during my retirement years)

          QList<QString> holderList;
          int timeout = 500;
          QTimer timer;
          timer.start();
          
          if (timer.elapsed() != 0)
          {
                holderList.append("test-string");
          }
          

          I'm looking to limit the appending to the QList to 500 ms. Once that limit is hit, stop appending. Is this possible?

          Thanks for the help and any suggestions.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @Calicoder said in QTimer used to time adding to a QList for specified time?:

          I'm looking to limit the appending to the QList to 500 ms. Once that limit is hit, stop appending. Is this possible?

          I am not sure to really understand your use case.
          If you want to measure elapsed time, why not simple use QElapsedTimer?

          QList<QString> holderList;
          int timeout = 500;
          QElapsedTimer timer;
          timer.start();
          
          while(timer.elapsed() < timeout )
          {
                holderList.append("test-string");
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          4
          • C Calicoder

            Howdy QT'ers, wondering if anyone knows if it's possible to time when items are added to a QList using a QTimer? I've come up with this pseudocode so far: (slowly teaching myself C++ and QT coding thanks to you guys during my retirement years)

            QList<QString> holderList;
            int timeout = 500;
            QTimer timer;
            timer.start();
            
            if (timer.elapsed() != 0)
            {
                  holderList.append("test-string");
            }
            

            I'm looking to limit the appending to the QList to 500 ms. Once that limit is hit, stop appending. Is this possible?

            Thanks for the help and any suggestions.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @Calicoder since you're a beginner, let me give you an example as complex and convoluted as possible.... 🤣

            int main(int argc, char *argv[])
            {
                QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                QApplication app(argc, argv);
            
                QTimer timerToAddtoList;
                QElapsedTimer elapsedTime;
            
                QList<qint64> listToAppendTo;
            
                QObject::connect(&timerToAddtoList, &QTimer::timeout, [&]()->void{
                        listToAppendTo.append(elapsedTime.elapsed());
                        if(elapsedTime.elapsed() > 500){ //500 ms
                            timerToAddtoList.stop();
                            qDebug() << listToAppendTo;
                            QCoreApplication::quit();
                        }
                    });
            
                elapsedTime.start();
                timerToAddtoList.start(0); // 0 == 0ms == fire as soon as possible
            
                return app.exec();
            }
            

            No seriously this contains everything you want to have, all in main. With some exotic stuff sprinkled in, to make it happen.

            Could be a good "exercise" to break this up in a proper class or something similar!

            If I should explain more, tell me so :D


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            4

            • Login

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