wait for a db field set...
-
Hi all,
at my application I need to wait for a db field gets a value... my code is like this.
bool my_must_wait_func(int a_Var) { RECHECK: aQuery("select afield from aTable Where Afield = a_Var); if (!aQuery.value("afield").tobool ) { qApp->processEvents(QEventLoop::AllEvents); goto RECHECK; } else { return true; } }
This func kills my database.. if I can query my db on every 250 ms it's enough for me..
I couldnt manage it by timers.. they're going out of my func..
Please Help...
-
Do you really have to wait "actively" in a function?
You can use QTimer::singleShot() in the slot called after timeout you check the db value if it is not there you fire QTimer::singleShot() again. If the db field was changed you do what ever needs to be done and don't fire QTimer::singleShot() anymore. -
@zeroptr
Hi
You can maybe use a lambda if u have c++ 11.void MyClass::SomeMethod(int a) {
QTimer::singleShot(1000, { code..; } );
}That will stay in same function sort of.
Waiting in function in event driven applications like Qt is often just a bad idea. even when using qApp->processEvents.
-
@zeroptr
hi
After 1 sec (1000) it will call the "code" section.But why must u wait -IN- the function?
Normal design would be using timer or thread to look in database over and over. and when seen value it would emit signal and the code
that needs the value would run in the called slot.