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...


  • Moderators

    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.



  • unfortunately yes...

    I have to be in that function...


  • Qt Champions 2016

    @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.



  • Hi @mrjj ,

    I don't think that single shot waits, until shot accures ?

    thanks for your help & idea..


  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.