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. Simple While loop without classes or events?
QtWS25 Last Chance

Simple While loop without classes or events?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 6 Posters 3.5k 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.
  • I Offline
    I Offline
    Injunear
    wrote on last edited by
    #1

    Hello,

    I have been trying to use Qt for many months. I have created a three page GUI for my project. I am not a C++ programmer but I understand the basics.

    I am wanting to know how to create a simple While loop to run my program. I just want to read a few GPIO inputs (external switches and button presses) and write to a few GPIO outputs on a machine I am designing.

    Is this possible with Qt? I understand how to do it in C++ but I also need a GUI which is why I selected Qt.

    Thank you for your help.

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

      Hi,

      What would that while loop do exactly ?

      Depending on that, you might not need one.

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

      I 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What would that while loop do exactly ?

        Depending on that, you might not need one.

        I Offline
        I Offline
        Injunear
        wrote on last edited by
        #3

        @SGaist The While loop will read switch inputs on GPIOs and button presses on the GUI and then write to output GPIOs and it will run forever.

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

          Depending on the refresh rate you need, a QTimer could be enough to do what you want.

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

          I 1 Reply Last reply
          1
          • SGaistS SGaist

            Depending on the refresh rate you need, a QTimer could be enough to do what you want.

            I Offline
            I Offline
            Injunear
            wrote on last edited by
            #5

            @SGaist It should loop at least 15 - 20 times a second. Is there a way to create a loop ( as in regular C programming) that will do this?

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

              Sure, you can subclass QThread and reimplement the run method with your loop.

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

              I 1 Reply Last reply
              2
              • SGaistS SGaist

                Sure, you can subclass QThread and reimplement the run method with your loop.

                I Offline
                I Offline
                Injunear
                wrote on last edited by
                #7

                @SGaist Thank you. I was hoping to avoid threads too, but if there is no other way, I'll have to learn how to use threads.

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

                  Hi
                  Just a note.
                  The reason we talk about Threads is that plain looping (while/for) strangulate the main event loop and makes application unresponsive.
                  Its sometimes possible to help the situation a little by calling
                  QApplication::processEvents() in the loop but
                  its not good design and in the long run, using a thread will just work
                  so much better overall.

                  I know looking at the docs seems a bit involved but its not so bad after fiddling with it for some time.
                  I personally liked this tut
                  http://www.bogotobogo.com/Qt/Qt5_QThreads_Creating_Threads.php
                  Please notice that besides overriding the run method, there is also the worker approach.
                  https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                  1 Reply Last reply
                  2
                  • I Injunear

                    @SGaist Thank you. I was hoping to avoid threads too, but if there is no other way, I'll have to learn how to use threads.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi @Injunear,

                    If you do GUI programming, you can't just have endless loops as your app needs to process events, like user clicks, window movements, system color changes and so on.

                    So the way to go is really either using a QThread as suggested by @SGaist or to create a second thread (beside the main thread which handles all GUI events).

                    For 20 repeats per second I'd go with a QTimer (I guess your function will execute quite fast).

                    All you need is a slot function that is effectively the "body" of your loop, a QTimer with intervall 50 ms, and a connect that binds both together. Very basic, no threads involved.

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    4
                    • AndeolA Offline
                      AndeolA Offline
                      Andeol
                      wrote on last edited by
                      #10

                      Yep, I strongly concur with mrjj on this. In particular if you are new to Qt, there is no need for threads.
                      Threads are very nice, and maybe one day, you'll need some, so it's not lost time to learn how to use them. But for what you want, a QTimer is enough. Put the content of your loop in a slot. And connect the QTimer timeout() signal to this slot.

                      From the moment you call start(50) on your timer, the slot will be executed every 50ms.

                      Threads become useful if the content of your slot becomes a bit heavy.

                      Developer for R++ : https://rplusplus.com/

                      1 Reply Last reply
                      2
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #11

                        Since nobody mentioned it yet. event loops are started calling QCoreApplicatio::exec() or QEventLoop::exec() or QThread::exec()

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        3
                        • I Offline
                          I Offline
                          Injunear
                          wrote on last edited by
                          #12

                          Thank you all for your help. I have been able to create a thread with my loop inside and it reads the buttons pushed on the GUI.

                          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