0

I have a global variable that i set on a cpp file on my qt project. I want to check this variable in every 100ms for 5 second and if the variable is 0 after 5 seconds I want to create a message box. Here is the sample of my code:

db.cpp:

if(case){
  g_clickedObj.readFlag = 1 ;
}
else{
g_clickedObj.readFlag = 0 ;
    }

mainwindow.cpp

this->tmr = new QTimer();

connect(this->tmr,SIGNAL(timeout()),this,SLOT(callSearchMachine()));

tmr->start(5000); 
  • 1
    `g_clickedObj.readFlag == 1 ;` -- That can't be the code, since you are using `==` instead of `=`. – PaulMcKenzie Sep 18 '20 at 14:31
  • Something like this? [SO: QTimer::timeout isn't firing](https://stackoverflow.com/a/54840605/7478597) – Scheff's Cat Sep 18 '20 at 14:35
  • If you set the variable in same thread where the timer is, I don't see why you need a `QTimer` to periodically check its value. Instead, setting the variable could emit a signal, and the slot (connected to this signal) may start the timer to wait 5 seconds until message box is popped. If the variable could/would be set in another thread that would be a different story... [SO: How to alter Qt Widgets in WINAPI threads?](https://stackoverflow.com/a/61750145/7478597) – Scheff's Cat Sep 18 '20 at 14:40

2 Answers2

0

Does this work for you? I didn't see a need to check every 100ms, if readFlag doesn't get set to 1 in 5 seconds, you'll output an error message, otherwise nothing happens.

    // where you want to start the 5 second count down...
    QTimer::singleShot(5000, this, SLOT(maybePrintAnErrorMsg()));
    . . .

    // the slot:
    void MyClass::maybePrintAnErrorMsg()
    {
      if (readFlag != 1) {
        // show a QMessageBox;
      }
    }

    . . . somewhere else in your code, when you set readFlag to 1:

    if (case) {
      // when the timer goes off, we won't print an error message
      readFlag = 1;
    }
0

Option 1: Use a timer with 100ms interval to check global variable, hold a member variable for counting how many times timer slot called. When slot called 5000/100=50 times, stop timer and create message box if necessary.

void MyClass::onTimeout(){
    // check variable
    // increment counter
    // check if counter reached 5000/100
    // if so stop timer and create message box
}

Option 2: Use two counters(one with 100ms interval, other with 5000ms) which has two different slots. Start both counters at same time. Let 100ms timer's slot check global variable, let 5000ms timer's slot stop both timers and check global variable, create message box if necessary.

zziyaa
  • 51
  • 2
  • Can these options work while my other functions of app running? I have a communication function on my app and that function has to run all the time during app runs. I tried both options but my communication function stopped working until timer timeout. – trytobedeveloper Sep 21 '20 at 08:09
  • In that case you should move QTimer part to another thread. You can check this https://stackoverflow.com/a/19252927/12122790 for it. – zziyaa Sep 21 '20 at 09:03