Display update and infinite loop



  • Ok im new and im not english anyway i hope i ll be understood !!!

    I have to code a GUI which must do :
    -receive message from a seriel link
    -update the color of some buttons in function of the message !

    for example i receive the 2 carac '1g', then i must color the button 1 in green. That's the idea !
    I had no porb to code the reception part ! Ihave no prob with the coloration !
    I have prob because i must receive to the infinite ! then i wrote a while loop in a slot which is called by a button but the like
    i don't get out of it ! its normal that the program crashes, but it don't even colored the button !

    here is the funciton :
    @
    void fdechange::event_loop(void) {
    goto passage;
    while(historique[0]!=0 && reception.first == true) {
    //supposed to be infinite exept if we turn off the button ! (reception.first is the stat of the button)
    passage:
    historique+=ABmessage::reception_message(idFE)); // here is the reception and the coloration
    if(historique[0] != 0)
    historique[0]->afficher_message();
    }
    }
    @
    So i think that would be normal that while i dont come back in the main it don't color the results of the message but how must i do it ?

    is the thread the solution ? cause i did some research before coming here and it seems to be fitting my problem but i don't know

    is my problem clear ?

    thank you foreward for helping me !

    [edit: please use @ tags around your code, thanks. eddy]



  • Create a QTimer with the desired interval and connect a slot that contains your polling code to it. Then start the timer. If you want your polling to be executed as frequent as possible, use zero as interval.

    This has the benefit that the timer does repeated calls to the slot while keeping the Qt event loop running, i.e. UI changes/interactions may happen or any other events. As you said, an infinite while loop just locks the thread in that short segment of code and thus blocks all this.

    Further you might want to look for a possibility to not have a polling mechanism in the first place. Polling continuously asks the serial port whether there's data. Instead, have the serial port notify you that there now is data available. Polling usually is a technique frowned upon, so if it's avoidable (i.e. your serial library allows callbacks or similar), avoid it. The reason is that the polling creates unnecessary CPU load (if done vigorously, even 100% load on one core), while having the serial report via event/callback, basically leaves your application completely idle while waiting.



  • In fact i didn't have time to do other than the logical way : thread !
    I will change the way later now i just need it to work and it is working by the way !
    Thank you for the real way i will implement it !



  • Yes, using a second thread for polling is absolutely okay, too. I just thought it would be easier for someone new in Qt to do it the QTimer way instead of the QThread way. But if you managed with QThread, great! :)

    (Of course, you should feel a vague sense of shame for CPU intensive polling, but if the serial library doesn't provide callbacks/events, you can't do it differently.)


Log in to reply
 

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