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

Program "Crashes" when using a while loop with a checked button



  • I am making a basic program to learn more about Qt and to get used to the workflow.
    What i want to achieve is that when i press a button it starts a loop until the button is pressed again.
    In c++ itself this could be done with a while loop that i set to true untill you press the hotkey again.

    But that doesn't work as easy here. When i set the while loop to true that program "Crashes" because its stuck in the while loop so its cant do anything.

    My Code:

    void TestApp::on_pushButton_3_toggled(bool checked)
    {
        if (checked) {
            ui->status_label->setText("Status: Testing!");
            while (true) {
                qDebug() <<"Test";
            }
        } else {
            ui->status_label->setText("Status: Working!");
        }
    }
    

    So how can i start the loop when i press the button and how can i stop it again when i press it again.
    And (not my first priority) how can i make like a hotkey system where i can set a button like Right Shift so when i press that it starts the loop.



  • @MrBeep Hello and welcome, your program is not crashing... it is just locked in an endless loop!
    Doing this, you lock the main thread, which is the GUI thread, so you can do anything else with your software.
    Why do you want to loop forever?



  • @MrBeep

    What i want to achieve is that when i press a button it starts a loop until the button is pressed again.

    In a simple word, this is not how Qt or event driven libraries work. You will not use any while loop in the way you are currently trying to do.

    You must read how Qt signals and slots (https://doc.qt.io/qt-5/signalsandslots.html) work, and follow that pattern.



  • @KroMignon Hi,
    I dont really need an while loop but since that worked in a c++ console app thats what i tought of here.
    The thing i want is just a loop that starts and stops on a button press / hotkey press



  • @MrBeep said in Program "Crashes" when using a while loop with a checked button:

    The thing i want is just a loop that starts and stops on a button press / hotkey press

    Hmm, this don't make sense to me, in a HMI/GUI point of view. Doing a while loop on a QThread will break the Qt signals/slots mechanism. To work, signals/slots needs a working event queue, read Threads and QObjects for more details about thread with Qt.

    If you want to made work the while loop you can do it like this (but it as in general a bad practice):

    void TestApp::on_pushButton_3_toggled(bool checked)
    {
        if (checked) {
            ui->status_label->setText("Status: Testing!");
            while (true) {
                // allow QThread to process events
                this->thread()->eventDispatcher()->processEvents(QEventLoop::AllEvents);
                qDebug() <<"Test";
            }
        } else {
            ui->status_label->setText("Status: Working!");
        }
    }
    


  • @KroMignon Alright thanks for the help!


  • Lifetime Qt Champion

    Hi,

    What does that thread do ?
    Did you implement it so that it is interruptible ?


Log in to reply