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 crash using a while loop
Forum Update on Tuesday, May 27th 2025

Program crash using a while loop

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 5.1k Views 2 Watching
  • 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.
  • A anfedres

    @Wieland said in Program crash using a while loop:

    Hi, and welcome to the Qt forum. Once your program enters the while loop, it's trapped there forever. And as you're executing this infinite loop on the GUI thread, your GUI can't respond to clicks (or do anything else) anymore.

    How could I solve this problem? How could I run something continuously and be able to check and get out of that loop?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #5

    @anfedres As alternative to what @Wieland suggested you could do it in Qt way: use signals/slots.
    This way you avoid multi-threading which is a complex and error prone topic.
    Implement a slot and connect it to the http://doc.qt.io/qt-5/qiodevice.html#readyRead signal of your serial port. In that slot then call readAll.

    One note:

    ui->textEdit->setText((QString)datas);
    

    this will overwrite what was set before in textEdit, not sure whether it is intended behaviour. If you want to see everything you got so far do:

    ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);
    

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    A 1 Reply Last reply
    1
    • jsulmJ jsulm

      @anfedres As alternative to what @Wieland suggested you could do it in Qt way: use signals/slots.
      This way you avoid multi-threading which is a complex and error prone topic.
      Implement a slot and connect it to the http://doc.qt.io/qt-5/qiodevice.html#readyRead signal of your serial port. In that slot then call readAll.

      One note:

      ui->textEdit->setText((QString)datas);
      

      this will overwrite what was set before in textEdit, not sure whether it is intended behaviour. If you want to see everything you got so far do:

      ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);
      
      A Offline
      A Offline
      anfedres
      wrote on last edited by
      #6

      @jsulm

      ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);

      That line really helped me a lot. I was planning to build an array, save it and display it.

      Talking about the Signals and Slots, I just started to program in qt yesterday, I'm getting familiar with it. I was trying to implement a toggle button but failed. The idea is having a button that if you press for the first time, stays down and sends data using the UART. I had problems finding the right example using "connect".

      I have done the following, which it works, but the communication gets data when is available, I was looking more to get the data if I press the button.

      The following line will connect the port to the signal when there is data available.

      connect(&Totem_Serial,SIGNAL(readyRead()),this,SLOT(Totem_Serial_Received()));
      

      Where Totem_Serial_Received() is defined as follow:

      void MainWindow::Totem_Serial_Received()
      {
          QByteArray datas = Totem_Serial.readAll();
      //    ui->textEdit->setText((QString)datas);
          ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);
          qDebug()<<"Im here";
          qDebug()<<datas;
      }
      

      From here, connecting the signal of the bottom being down shouldn't be that hard. If you point me out an example, I will appreciate it.

      Thank you for your help.

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

        Hi
        For a button that acts like a checkbox please see
        http://doc.qt.io/qt-5/qabstractbutton.html#checkable-prop
        and the Toggle signal
        http://doc.qt.io/qt-5/qabstractbutton.html#toggle

        so you would enable checking

        ui->TheButton->setCheckable(true);

        and then use connect
        QObject::connect(ui->TheButton, SIGNAL(toggled(bool)), Mainwindow, SLOT(yourslot(bool)));

        Remember to add "yourslot" to mainwindow

        1 Reply Last reply
        1
        • A Offline
          A Offline
          anfedres
          wrote on last edited by
          #8

          @mrjj I'll might need a litte bit more of help. I'm really trying, but. I'm still to new to this. I'm not quite sure what am I doing wrong here. Signals/Slots should't be that hard.

          The following code should make the pushbutton checkeable, so it will remain pressed once you pressed, and it will be released once you presed it again. While the button is pressed, checked will remain with a Boolean state (I'm guessing it will be true). So, my signal will be generated by the object pusshButton, such signal will be "checked" (the stated of the button). The previous signal will activate the Totem_Serial_Received function. Am I right?

           ui->pushButton->setCheckable(true);
              QObject::connect(ui->pushButton, SIGNAL(checked), this, SLOT(Totem_Serial_Received()));
          

          Where Totem_Serial_Received() is as follow:

          void MainWindow::Totem_Serial_Received()
          {
          
              QByteArray datas = Totem_Serial.readAll();
              ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);
              qDebug()<<"Im here";
              qDebug()<<datas;
          }
          

          And the button was defined as follow:

          void MainWindow::on_pushButton_toggled(bool checked)
          {
            
          }
          

          I'm also getting an error: QObject::connect: Parentheses expected, signal QPushButton::checked in ..\Totem_app\mainwindow.cpp:35
          I think I have all the parentheses.

          Thanks for your help. I'm sorry if the questions seems simply, but I just started to program in qt yesterday.

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #9

            Hi
            Np. as long as you try, its ok to ask anything you like.

            I think we need to talk about the logic.

            The check button signal should not send to Totem_Serial_Received as that is for reading data.

            so its correct to hook it up to
            void MainWindow::on_pushButton_toggled(bool checked)
            {
            if (checked) // ok to read data
            else
            // not ok
            }

            But do you want to close the serial completely or simply ignore what is sent ?

            Because there are many ways to implement this so how do you really want it to work?

            Update:
            Higher up you write ", stays down and sends data using the UART."

            So maybe i misread.

            This toggle buttons controls sending stuff ? ( not reading) ?
            Also it wont call a function over and over while being down.
            Do you need that?

            1 Reply Last reply
            1
            • A Offline
              A Offline
              anfedres
              wrote on last edited by anfedres
              #10

              @mrjj What I want is just to read data when I press the button, and stop reading data when I press it again. Is it the connect implementation OK? But I want is to read data while the button is holding its state of pressed.

              This toggle buttons controls sending stuff ? ( not reading) ?

              I'm reading data using the UART

              Also it wont call a function over and over while being down.
              Do you need that?

              That's what I'm trying to implement, read data continuously while the button is down, that's why I implemented it at the beginning with the while loop.

              mrjjM 1 Reply Last reply
              0
              • A anfedres

                @mrjj What I want is just to read data when I press the button, and stop reading data when I press it again. Is it the connect implementation OK? But I want is to read data while the button is holding its state of pressed.

                This toggle buttons controls sending stuff ? ( not reading) ?

                I'm reading data using the UART

                Also it wont call a function over and over while being down.
                Do you need that?

                That's what I'm trying to implement, read data continuously while the button is down, that's why I implemented it at the beginning with the while loop.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #11

                @anfedres said in Program crash using a while loop:

                read data continuously while the button is down,

                The QSerial is already reading by it self., if it sees data then it will call the SLOT. (Totem_Serial_Received)

                So if you mean to completely stop reading, then you need to close the serial port or disconnect the data signal.

                Im not sure why you need this. Maybe its just the wording.

                So if Checked, then serial port will be oepn and data is being read.
                If dechecked, then serial port is closed. Is that what u mean ?

                Else im not sure where you are going with this checkable button :)

                A 1 Reply Last reply
                1
                • mrjjM mrjj

                  @anfedres said in Program crash using a while loop:

                  read data continuously while the button is down,

                  The QSerial is already reading by it self., if it sees data then it will call the SLOT. (Totem_Serial_Received)

                  So if you mean to completely stop reading, then you need to close the serial port or disconnect the data signal.

                  Im not sure why you need this. Maybe its just the wording.

                  So if Checked, then serial port will be oepn and data is being read.
                  If dechecked, then serial port is closed. Is that what u mean ?

                  Else im not sure where you are going with this checkable button :)

                  A Offline
                  A Offline
                  anfedres
                  wrote on last edited by
                  #12

                  @mrjj You might be right. Yes, what about, displaying the data! I think that would be the right think to do. My guess would be, instead of calling

                  Totem_Serial_Received()
                  

                  I should call a function that does the following while the button is down.

                  ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);
                  

                  This sounds like a more adequate approach. Am I right?

                  mrjjM 1 Reply Last reply
                  0
                  • A anfedres

                    @mrjj You might be right. Yes, what about, displaying the data! I think that would be the right think to do. My guess would be, instead of calling

                    Totem_Serial_Received()
                    

                    I should call a function that does the following while the button is down.

                    ui->textEdit->setText(ui->textEdit->toPlainText() + (QString)datas);
                    

                    This sounds like a more adequate approach. Am I right?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @anfedres
                    Hi
                    For just the data display, you can just do

                    void MainWindow::on_pushButton_toggled(bool checked)
                    {
                    ui->textEdit->setVisible(checked);
                    }

                    Then it will hide the textEdit if not checked :)

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      anfedres
                      wrote on last edited by
                      #14

                      Many thanks to all of you for your help.

                      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