How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop
-
-
Thanks @mpergand i'm trying my best
Actually, i want my code to run the traitement inside Qtimer befor it moves to the next if condition.
Could i put the traitement inside this proceed ?void proceedNextStep() // slot { // do step work // if no more step return; // proceed next step QTimer::singleShot(6000,this, proceedNextStep); }
and from where i get "LongDelayTimeOut" is it known by Qt ?
-
@mpergand is "_counter++" an integer ?
in my case for(i=0, i< selection.count(), i++) so selection.count()=_counter?
and what _currentState takes initially ?insteed of
for(int i=0; i< selection.count(); i++)
i will put
while(_counter!=0){_counter--}
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
while(_counter!=0){_counter--}
You can't have a loop, neither for nor while.
To eliminate the counter variable, you can put all the process informations you have to send in a queue (ex: QQueue) and call nextstep untill the queue is empty.QQueue _proceedInfo; // instance variable ... void proceedNextStep() // slot { if(_processInfo.empty()) return; // no more process // do step work QString info=_processInfo.dequeue(); if(info=="F12B")) // special case { QTimer::singleShot(6000,this, [this]() { // long delay timeout ... // next step QTimer::singleShot(0,this, proceedNextStep); }); return; } // do other process ... // proceed next step QTimer::singleShot(0,this, proceedNextStep); }
-
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
for(int i=0; i< selection.count(); i++)
@mpergand can i fill QQueue _proceedInfo this way ?
QQueue _proceedInfo; for(int i=0; i< selection.count(); i++) { _proceedInfo=i; } void proceedNextStep() // slot { if(_processInfo.empty()) return; // no more process // do step work QString info=_processInfo.dequeue(); if(info=="F12B")) // special case { QTimer::singleShot(6000,this, [this]() { // long delay timeout ... // next step QTimer::singleShot(0,this, proceedNextStep); }); return; } // do other process ... // proceed next step QTimer::singleShot(0,this, proceedNextStep); }
and look what i get here:
-
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
for(int i=0; i< selection.count(); i++)
@mpergand can i fill QQueue _proceedInfo this way ?
QQueue _proceedInfo; for(int i=0; i< selection.count(); i++) { _proceedInfo=i; } void proceedNextStep() // slot { if(_processInfo.empty()) return; // no more process // do step work QString info=_processInfo.dequeue(); if(info=="F12B")) // special case { QTimer::singleShot(6000,this, [this]() { // long delay timeout ... // next step QTimer::singleShot(0,this, proceedNextStep); }); return; } // do other process ... // proceed next step QTimer::singleShot(0,this, proceedNextStep); }
and look what i get here:
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
can i fill QQueue _proceedInfo this way ?
Please read documentation: https://doc.qt.io/qt-6/qqueue.html
To add elements to the queue you call enqueue() as shown there.Regarding error: what you are doing is wrong. When you call singleShot you do NOT call the member function but pass its address:
QTimer::singleShot(100, this, &MainWindow::processStep);
-
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
for(int i=0; i< selection.count(); i++)
@mpergand can i fill QQueue _proceedInfo this way ?
QQueue _proceedInfo; for(int i=0; i< selection.count(); i++) { _proceedInfo=i; } void proceedNextStep() // slot { if(_processInfo.empty()) return; // no more process // do step work QString info=_processInfo.dequeue(); if(info=="F12B")) // special case { QTimer::singleShot(6000,this, [this]() { // long delay timeout ... // next step QTimer::singleShot(0,this, proceedNextStep); }); return; } // do other process ... // proceed next step QTimer::singleShot(0,this, proceedNextStep); }
and look what i get here:
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
can i fill QQueue _proceedInfo this way ?
QQueue is a tempate class, see the doc.
try: QTimer::singleShot(0,this, SLOT(proceedNextStep));
Remember what i write is pseudo code. -
@jsulm
i found this code here https://doc.qt.io/qt-6/qqueue.htmlQQueue<int> queue; queue.enqueue(1); queue.enqueue(2); queue.enqueue(3);
i fixe it this way:
QQueue<int> _proceedInfo; for(int i=0; i< selection.count(); i++) { _proceedInfo.enqueue(i); }
-
@mpergand in this case in your code :
QQueue _proceedInfo; // instance variable ... void proceedNextStep() // slot { if(_processInfo.empty()) return; // no more process // do step work QString info=_processInfo.dequeue(); if(info=="F12B")) // special case { QTimer::singleShot(6000,this, [this]() { // long delay timeout ... // next step QTimer::singleShot(0,this, proceedNextStep); }); return; } // do other process ... // proceed next step QTimer::singleShot(0,this, proceedNextStep); }
No need for enum variable ? they doesn't exist here ?
-
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
and from where i get "LongDelayTimeOut" is it known by Qt ?
You have to replace the for loop with a counter and to define the different states you need:
enum { Proceed_next, LongDelayTimeOut, ... }
as well as an instance variable to store the current operation status:
if(_currentState==LongDelayTimeOut) { // long delay ending } else if(_currentState==Proceed_next) { _counter++; // get next payload if (PayloadData_R=="F12B") { _currentState=LongDelayTimeOut; QTimer::singleShot(6000, this, proceedNextStep); return; } // next payload _currentState=Proceed_next; // default state QTimer::singleShot(100,this, proceedNextStep); } }
Keep in mind that you need to know what to do each time proceedNext is call, so you need to carefuly define the different states of the current operation.
@mpergand is this code have to be inside void proceedNextStep(){...} ?
if(_currentState==LongDelayTimeOut) { // long delay ending } else if(_currentState==Proceed_next) { _counter++; // get next payload if (PayloadData_R=="F12B") { _currentState=LongDelayTimeOut; QTimer::singleShot(6000, this, proceedNextStep); return; } // next payload _currentState=Proceed_next; // default state QTimer::singleShot(100,this, proceedNextStep); } }
-
@mpergand in this case in your code :
QQueue _proceedInfo; // instance variable ... void proceedNextStep() // slot { if(_processInfo.empty()) return; // no more process // do step work QString info=_processInfo.dequeue(); if(info=="F12B")) // special case { QTimer::singleShot(6000,this, [this]() { // long delay timeout ... // next step QTimer::singleShot(0,this, proceedNextStep); }); return; } // do other process ... // proceed next step QTimer::singleShot(0,this, proceedNextStep); }
No need for enum variable ? they doesn't exist here ?
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
No need for enum variable ?
Yes if it is all you have to do.
I still don't understand why you want to store number instead of the data you really need:
QString PayloadData_R = m_ui->Table_CSV->model()->data(m_ui->Table_CSV->model()->index(b,1)).toString();
Put that string directly in the queue.
-
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
No need for enum variable ?
Yes if it is all you have to do.
I still don't understand why you want to store number instead of the data you really need:
QString PayloadData_R = m_ui->Table_CSV->model()->data(m_ui->Table_CSV->model()->index(b,1)).toString();
Put that string directly in the queue.
@mpergand Because i need "b" inside index(b,1)
i get it this way:for(int i=0; i< selection.count(); i++) { QModelIndex ind = selection.at(i);//at returns item at the index position i qDebug() << "QModelIndex ind = selection.at(i);" << ind; int a= ind.column(); //Returns the column this model index refers to qDebug() << "a=" << a; int b= ind.row(); //Returns the row this model index refers to qDebug() << "b=" << b; //indice ligne //getting data from selected row QString PayloadData_R = m_ui->Table_CSV->model()->data(m_ui->Table_CSV->model()->index(b,1)).toString(); qDebug() <<"PayloadData_R sendrow="<<PayloadData_R;
-
The idea is to put the data you need in the queue before calling proceedNext:
for(int i=0; i< selection.count(); i++) { QModelIndex ind = selection.at(i);//at returns item at the index position i // bla bla _proceedInfo.enqueue(PayloadData_R); } // start sending data proceedNext();
-
I want to put this function inside connect but it don't work;
void MainWindow::sendFrame(int i)connect(m_speedTimer,&QTimer::timeout, this, &MainWindow::sendFrame);
-
This is my sendFrame function how can i pass it as a slot with its parameter that changes each timeout ?
class MainWindow : public QMainWindow { .... private slots: void sendFrame(QString IdData_R,QString PayloadData_R,int i); }
@imene said in How can i set time out condition in the timer? Duplicated : https://forum.qt.io/topic/138913/qtimer-make-traitement-out-of-for-loop:
how can i pass it as a slot with its parameter that changes each timeout
I already told you how: "You can use a lambda as slot to work around this"...