Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program "Crashes" when using a while loop with a checked button
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
while loop
7 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrBeep
    wrote on last edited by
    #1

    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.

    KroMignonK JonBJ 2 Replies Last reply
    0
    • M MrBeep

      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.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

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

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      M 1 Reply Last reply
      1
      • M MrBeep

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

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

        1 Reply Last reply
        2
        • KroMignonK KroMignon

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

          M Offline
          M Offline
          MrBeep
          wrote on last edited by
          #4

          @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

          KroMignonK 1 Reply Last reply
          0
          • M MrBeep

            @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

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @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!");
                }
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            M 1 Reply Last reply
            1
            • KroMignonK KroMignon

              @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!");
                  }
              }
              
              M Offline
              M Offline
              MrBeep
              wrote on last edited by
              #6

              @KroMignon Alright thanks for the help!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved