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. Qt main loop
Forum Updated to NodeBB v4.3 + New Features

Qt main loop

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 3.7k 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 agmar

    @JonB Where do i put the qDebug() << test; then? Because i think putting it in the brackets will give nothing useful

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #9

    @agmar
    You need to write code that will be executing while the main loop is running. You cannot do this anywhere in the code you show. You need e.g. to create a QSerialPort (perhaps as a member variable in MainWindow) and attach slots to its signals (e.g. readyRead()), that is where you can put output etc, statements.

    A 1 Reply Last reply
    0
    • JonBJ JonB

      @agmar
      You need to write code that will be executing while the main loop is running. You cannot do this anywhere in the code you show. You need e.g. to create a QSerialPort (perhaps as a member variable in MainWindow) and attach slots to its signals (e.g. readyRead()), that is where you can put output etc, statements.

      A Offline
      A Offline
      agmar
      wrote on last edited by
      #10

      @JonB is it not possible to have a loop running? this should not be associated with any serial port, this is a seperate issue that i want to tackle...

      ideally, i would be able to have the following:

      int main(){

      some setup code

      while(1){
      code that runs perpetually, until return 0;
      }
      }

      void some_interrupt();

      the some_interrupt is code that would execute after some external event would trigger a pin on an mcu or something, and after it finishes, the while(1) would continue where it left off.

      In my case for Qt, i want to have something similar to the while(1), with signals having the ability to execute mid (while) loop and resume the code that was running in the while loop, is this a thing that can be done?

      jsulmJ JonBJ 2 Replies Last reply
      0
      • A agmar

        @JonB is it not possible to have a loop running? this should not be associated with any serial port, this is a seperate issue that i want to tackle...

        ideally, i would be able to have the following:

        int main(){

        some setup code

        while(1){
        code that runs perpetually, until return 0;
        }
        }

        void some_interrupt();

        the some_interrupt is code that would execute after some external event would trigger a pin on an mcu or something, and after it finishes, the while(1) would continue where it left off.

        In my case for Qt, i want to have something similar to the while(1), with signals having the ability to execute mid (while) loop and resume the code that was running in the while loop, is this a thing that can be done?

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #11

        @agmar said in Qt main loop:

        is it not possible to have a loop running?

        Why do you need such loop?
        You are using an asynchronous framework (Qt), there is no need for such loops most of the time.
        Please explain better what you are doing: do you read data from Arduino? If so how? In Qt you can simply use QSerialPort and don't need any blocking loops.

        If you really want such a loop you would need to move it to another thread to not to block your main thread. But that increases complexity a lot and should only be done if really needed and if you really know what you're doing.

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

        A 2 Replies Last reply
        0
        • A agmar

          @JonB is it not possible to have a loop running? this should not be associated with any serial port, this is a seperate issue that i want to tackle...

          ideally, i would be able to have the following:

          int main(){

          some setup code

          while(1){
          code that runs perpetually, until return 0;
          }
          }

          void some_interrupt();

          the some_interrupt is code that would execute after some external event would trigger a pin on an mcu or something, and after it finishes, the while(1) would continue where it left off.

          In my case for Qt, i want to have something similar to the while(1), with signals having the ability to execute mid (while) loop and resume the code that was running in the while loop, is this a thing that can be done?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #12

          @agmar
          You absolutely do not want to use that pattern, with a while (1) blocking loop in the main thread (which will stop the UI from being responsive), when using Qt/an event-driven system. Try to get out of that mindset.

          The pattern you want to use will look like:

          // in `MainWindow.h`, class `MainWindow`
          private:
              QSerialPort serial;
          private slots:
              void onReadyRead();
          
          // in `MainWindow.cpp`
          MainWindow::MainWindow(...)
          {
              serial.open(...);
              connect(&serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
          }
          
          void MainWindow::onReadyRead()
          {
              QByteArray bytes = serial.readAll();
              qDebug() << "Bytes read:" << bytes;
          }
          

          This means that any time new data arrives MainWindow::onReadyRead() will be called. the rest of the time the Qt main loop is just servicing whatever UI input arrives. It remains "responsive" rather than getting "blocked". Note that there is no "while loop" or "interrupts".

          A 1 Reply Last reply
          3
          • jsulmJ jsulm

            @agmar said in Qt main loop:

            is it not possible to have a loop running?

            Why do you need such loop?
            You are using an asynchronous framework (Qt), there is no need for such loops most of the time.
            Please explain better what you are doing: do you read data from Arduino? If so how? In Qt you can simply use QSerialPort and don't need any blocking loops.

            If you really want such a loop you would need to move it to another thread to not to block your main thread. But that increases complexity a lot and should only be done if really needed and if you really know what you're doing.

            A Offline
            A Offline
            agmar
            wrote on last edited by
            #13

            @jsulm like i mentioned, this is not related to a serialport or anything i have asked help for in the past, this is a seperate thing, however, this is exactly what i hope to do-
            C++ code found here:
            https://www.geeksforgeeks.org/signal-handling-in-cpp/

            // CPP Program to demonstrate the signal() function
            #include <csignal>
            #include <iostream>
            using namespace std;
              
            void signal_handler(int signal_num)
            {
                cout << "The interrupt signal is (" << signal_num
                     << "). \n";
              
                // It terminates the  program
                exit(signal_num);
            }
              
            int main()
            {
                // register signal SIGABRT and signal handler
                signal(SIGABRT, signal_handler);
              
                while (true)
                    cout << "Hello GeeksforGeeks..." << endl;
                return 0;
            }
            

            Output: Being in an infinite loop this code will show the following output until an interrupt is faced:

            
            Hello GeeksforGeeks...
            Hello GeeksforGeeks...
            Hello GeeksforGeeks...
            Hello GeeksforGeeks...
            

            Now if we press Ctrl+C to send an interrupt, the program will exit by printing:

            Hello GeeksforGeeks...
            Hello GeeksforGeeks...
            Hello GeeksforGeeks...
            Hello GeeksforGeeks...
            The interrupt signal is (22).
            

            the while(1) is blocking, sure, but it should not matter because an interrupt(signal) should execute mid execution of any while(1) line

            JonBJ 1 Reply Last reply
            0
            • JonBJ JonB

              @agmar
              You absolutely do not want to use that pattern, with a while (1) blocking loop in the main thread (which will stop the UI from being responsive), when using Qt/an event-driven system. Try to get out of that mindset.

              The pattern you want to use will look like:

              // in `MainWindow.h`, class `MainWindow`
              private:
                  QSerialPort serial;
              private slots:
                  void onReadyRead();
              
              // in `MainWindow.cpp`
              MainWindow::MainWindow(...)
              {
                  serial.open(...);
                  connect(&serial, &QSerialPort::readyRead, this, &MainWindow::onReadyRead);
              }
              
              void MainWindow::onReadyRead()
              {
                  QByteArray bytes = serial.readAll();
                  qDebug() << "Bytes read:" << bytes;
              }
              

              This means that any time new data arrives MainWindow::onReadyRead() will be called. the rest of the time the Qt main loop is just servicing whatever UI input arrives. It remains "responsive" rather than getting "blocked". Note that there is no "while loop" or "interrupts".

              A Offline
              A Offline
              agmar
              wrote on last edited by
              #14

              @JonB this is not for a serial port however, what you wrote makes perfect sense, but it is not the problem i want to solve, its separate from everything i needed help with before, my latest reply to jsulm should make sense, i hope

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @agmar said in Qt main loop:

                is it not possible to have a loop running?

                Why do you need such loop?
                You are using an asynchronous framework (Qt), there is no need for such loops most of the time.
                Please explain better what you are doing: do you read data from Arduino? If so how? In Qt you can simply use QSerialPort and don't need any blocking loops.

                If you really want such a loop you would need to move it to another thread to not to block your main thread. But that increases complexity a lot and should only be done if really needed and if you really know what you're doing.

                A Offline
                A Offline
                agmar
                wrote on last edited by
                #15

                @jsulm sorry for not answering : Why do you need such loop?

                i think it would be useful to know how to do, this might not be useful now , but this is one of the fundamentals to learn, for me, at least...

                jsulmJ 1 Reply Last reply
                0
                • A agmar

                  @jsulm like i mentioned, this is not related to a serialport or anything i have asked help for in the past, this is a seperate thing, however, this is exactly what i hope to do-
                  C++ code found here:
                  https://www.geeksforgeeks.org/signal-handling-in-cpp/

                  // CPP Program to demonstrate the signal() function
                  #include <csignal>
                  #include <iostream>
                  using namespace std;
                    
                  void signal_handler(int signal_num)
                  {
                      cout << "The interrupt signal is (" << signal_num
                           << "). \n";
                    
                      // It terminates the  program
                      exit(signal_num);
                  }
                    
                  int main()
                  {
                      // register signal SIGABRT and signal handler
                      signal(SIGABRT, signal_handler);
                    
                      while (true)
                          cout << "Hello GeeksforGeeks..." << endl;
                      return 0;
                  }
                  

                  Output: Being in an infinite loop this code will show the following output until an interrupt is faced:

                  
                  Hello GeeksforGeeks...
                  Hello GeeksforGeeks...
                  Hello GeeksforGeeks...
                  Hello GeeksforGeeks...
                  

                  Now if we press Ctrl+C to send an interrupt, the program will exit by printing:

                  Hello GeeksforGeeks...
                  Hello GeeksforGeeks...
                  Hello GeeksforGeeks...
                  Hello GeeksforGeeks...
                  The interrupt signal is (22).
                  

                  the while(1) is blocking, sure, but it should not matter because an interrupt(signal) should execute mid execution of any while(1) line

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #16

                  @agmar said in Qt main loop:

                  the while(1) is blocking, sure, but it should not matter because an interrupt(signal) should execute mid execution of any while(1) line

                  Your example is not a Qt program and is not for a Qt program. It is a pattern for a "command line" application and you are inappropriately copying it into an event-driven GUI application. If you want to write a Qt command-line-only program you can do so, but you are writing a UI one so not. This seems to be the crux of your (mis-)understanding.

                  In any case, what exactly are you trying to achieve? Why are trying to write an application with windows/a UI built around an example using Ctrl+C interrupt and couts?

                  1 Reply Last reply
                  0
                  • A agmar

                    @jsulm sorry for not answering : Why do you need such loop?

                    i think it would be useful to know how to do, this might not be useful now , but this is one of the fundamentals to learn, for me, at least...

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #17

                    @agmar said in Qt main loop:

                    i think it would be useful to know how to do, this might not be useful now

                    A loop is a loop not much to learn. Just write a loop.
                    What you really need to understand and learn is that you do not want to have endless (or long lasting) loops in an event driven application! If you put such a loop in main thread you block the Qt event loop and your app stops responding to user input or to process any signals. If, for whatever reason, you need such a loop in an event driven application you need to move it into another thread.

                    "the while(1) is blocking, sure, but it should not matter because an interrupt(signal) should execute mid execution of any while(1) line" - yes, and you can handle system signals in a Qt application in exact same way as you do in any other C/C++ application - install a handler for the system signal you want to handle...

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

                    JonBJ 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @agmar said in Qt main loop:

                      i think it would be useful to know how to do, this might not be useful now

                      A loop is a loop not much to learn. Just write a loop.
                      What you really need to understand and learn is that you do not want to have endless (or long lasting) loops in an event driven application! If you put such a loop in main thread you block the Qt event loop and your app stops responding to user input or to process any signals. If, for whatever reason, you need such a loop in an event driven application you need to move it into another thread.

                      "the while(1) is blocking, sure, but it should not matter because an interrupt(signal) should execute mid execution of any while(1) line" - yes, and you can handle system signals in a Qt application in exact same way as you do in any other C/C++ application - install a handler for the system signal you want to handle...

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #18

                      @jsulm said in Qt main loop:

                      yes, and you can handle system signals in a Qt application in exact same way as you do in any other C/C++ application - install a handler for the system signal you want to handle...

                      @agmar
                      ...But be aware that a Qt application with a UI is not going to receive any interrupt signal from pressing e.g. Ctrl+C in a window, that is a tty function and you are not using a tty here. Nor will your couts go anywhere useful....

                      1 Reply Last reply
                      0
                      • A agmar has marked this topic as solved on
                      • A agmar

                        @jsulm it would be nice to have something print out at supersonic speed, like in an arduino serial monitor without any delay, putting in a debug into the main loop does not seem to do that...

                        const char test = 'a';
                        
                        int main(int argc, char *argv[])
                        {
                        
                            QApplication a(argc, argv);
                            MainWindow w;
                            w.show();
                            return a.exec();
                            qDebug() << test;
                        }
                        
                        
                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #19

                        @agmar said in Qt main loop:

                        it would be nice to have something print out at supersonic speed

                        This is why your while(1) loop is wrong. Qt is event driven, like others have said already. QSerialPort has a signal that fires immediately when something arrives on the serial port (as "immediate" as possible). This signal needs to be connected to a slot that does the output. There is no noticeable delay in this approach. If you use cout for output, you need to make sure to flush the output immediately. Otherwise the buffer needs to be filled up before if flushes on its own. That indeed would be a delay.

                        1 Reply Last reply
                        0

                        • Login

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