QTimer used to time adding to a QList for specified time?
-
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.
-
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 -
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#detailsThanks 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?
-
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.
@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 useQElapsedTimer?QList<QString> holderList; int timeout = 500; QElapsedTimer timer; timer.start(); while(timer.elapsed() < timeout ) { holderList.append("test-string"); } -
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.
@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