Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Occasionly error with internal error When using QEventLoop



  • QThead thread; //call slots
    
    QEventLoop* eLoop;
    QTimer *timer;
    bool timeOutFlag=false;
    
    bool TimeOut()
    {        
            timer->start(interval);
            connect(&timer,QTimer::timeout,[]()
            {
                    eloop->quit();
                    timeOutFlag=true;
            })
            eloop->exec()
            return timeOutFlag;
    }
    
    /*
    Functionality:DoSomeThing once its according signal emits at specifi code running time.If there is not emitted singal during the specific code running time,skip DoSomeThing 
    */
    while(1)
    {
            if(TimeOut())
            {
                    break;
            }
            DoSomeThing();
    }
    

    Codes above is outline of my code using QEventLoop. It may error with "internal error"(i have read the soure of QEventloop but cannot understand the errorhint).Can you give me a solution or any suggestion?
    Thanks!


  • Lifetime Qt Champion

    @qiuyuFengye Can you please explain what you are trying to achieve? Using blocking loops and additional event loops is most of the time bad idea. Also, you connect the timeout signal each time you call Timeout() function!


  • Lifetime Qt Champion

    Don't use QEventLoop but signals/slots.



  • @jsulm DoSomeThing once its according signal emits at specifi code running time.If there is not emitted singal during the specific code running time,skip DoSomeThing


  • Lifetime Qt Champion

    @qiuyuFengye There is no need for a loop and event loop for that. Simply start the timer and connect that signal ("If there is not emitted singal") to a slot. In that slot check if the timer is still running, if it is call DoSomeThing.



  • @jsulm expain my purpose in detail:
    i have two device(host,slave).When needed ,i should let host communiction with slave(host send messgaes to slave,and slave responses it).According to slave's response,i decide to do something.
    Code is : host.send(msg),msgs=host.response(),process(msgs),AnyOtherFunction().I must keep the order , host communcations with slave and then AnyOtherFunction.
    when slave responses ,code emits response signal(connected to repsonsed slot).now my repsonsed slot is just storing responsed msg.


  • Lifetime Qt Champion

    @qiuyuFengye All this can be done with signals slots. You send message, you get response in a slot and process the response. Qt is asynchronous, you should not try to make it synchronous using loops and local event loops. Use signals/slots as they are meant to be used.



  • @jsulm My purpose is that host communcations with slave sequentially(communication1,communication2,communication3,...),the latter communication depends on former communication.if not blockig qthread,how
    should i achieve it?


Log in to reply