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. guidelines for designing an application
Qt 6.11 is out! See what's new in the release blog

guidelines for designing an application

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.2k Views 1 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I'm implementing a small program to communicate with an embedded device. I've begun implementing, and I notice that a lot of the work is being done in a slot, which strikes me as both bad practice and bad design.

    Here's what the app needs to do:

    1. allow the user to select a port
    2. connect to the port, send a notification request to the device through the port, and wait for a reply.
    3. parse the reply to determine some information about the device
    4. use this information to construct further messages to the device
    5. verify the correct reply
    6. exit on user command.

    This program will never be compute bound (or IO bound, for that matter)...it's going to spend most of its time waiting for input,either from the user or from the device port.
    Currently all my work is done in the slot which is called when a readyRead() signal is emitted from the port. Is it better practice to do something like set a flag that main() can see, and exit? Then, main could have a loop in which it checks this flag, and does the necessary processing.

    I'm looking for any general suggestions on proper application layout for this. Thanks...

    jsulmJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      I'm implementing a small program to communicate with an embedded device. I've begun implementing, and I notice that a lot of the work is being done in a slot, which strikes me as both bad practice and bad design.

      Here's what the app needs to do:

      1. allow the user to select a port
      2. connect to the port, send a notification request to the device through the port, and wait for a reply.
      3. parse the reply to determine some information about the device
      4. use this information to construct further messages to the device
      5. verify the correct reply
      6. exit on user command.

      This program will never be compute bound (or IO bound, for that matter)...it's going to spend most of its time waiting for input,either from the user or from the device port.
      Currently all my work is done in the slot which is called when a readyRead() signal is emitted from the port. Is it better practice to do something like set a flag that main() can see, and exit? Then, main could have a loop in which it checks this flag, and does the necessary processing.

      I'm looking for any general suggestions on proper application layout for this. Thanks...

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

      @mzimmers You can just have helper methods or classes which then do the work when called from the slot. If this work is time consuming you can move it to a second thread. Using a flag like you suggested isn't a clean solution and just moves the work to another place (where it does not belong).

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

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        I understand that in concept, but in practice, how do I notify a helper object from outside the slot? In my case the slot does #3 and #4 above. I'd like to move that work outside the slot.

        jsulmJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          I understand that in concept, but in practice, how do I notify a helper object from outside the slot? In my case the slot does #3 and #4 above. I'd like to move that work outside the slot.

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

          @mzimmers Why do you want to notify it? You just call it in the slot:

          void MainWindow::slot()
          {
              do_task_3();
              do_task_4();
          }
          

          If you use a helper object you can use signals/slots: MainWindow just emits signals in that slot and the helper object(s) connect to them and do what ever they need to do.

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

          1 Reply Last reply
          0
          • mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            But that's just moving the work to a subroutine; the work is still being done from the slot, right?

            Part of my reason for asking this question is that to me, a slot is like a callback in that you'd want to get out of it as quickly as possible. So...how do I shift the work from the slot to another area?

            jsulmJ 1 Reply Last reply
            0
            • mzimmersM mzimmers

              But that's just moving the work to a subroutine; the work is still being done from the slot, right?

              Part of my reason for asking this question is that to me, a slot is like a callback in that you'd want to get out of it as quickly as possible. So...how do I shift the work from the slot to another area?

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

              @mzimmers If you want to go out of slot as fast as possible than you need to use a thread to do the actual work.

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

              1 Reply Last reply
              1
              • mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                Hi jsulm - I'll look at some of the voidrealms tutorials on Qt threads again.

                Isn't is generally a good practice to try to get out of a slot as quickly as possible?

                jsulmJ 1 Reply Last reply
                0
                • mzimmersM mzimmers

                  Hi jsulm - I'll look at some of the voidrealms tutorials on Qt threads again.

                  Isn't is generally a good practice to try to get out of a slot as quickly as possible?

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

                  @mzimmers Sure, it is. As long as the slot is executing your main thread will be blocked - UI will not react and no other slots will be called.
                  For simple tasks in another thread you can use http://doc.qt.io/qt-5/qtconcurrent.html#run

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

                  1 Reply Last reply
                  2
                  • aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mzimmers said in guidelines for designing an application:

                    Isn't is generally a good practice to try to get out of a slot as quickly as possible?

                    I've heard this for interrupts as long as I'm programming. Fact is, in an interrupt routine do whatever needs to be done, but don't delay unneccessary.

                    Example: If your interrupt routine read serial port data to a buffer, then just add data to that buffer until the buffer is full or an end-of-line char is received. Then signal the main routine that the buffer can be processed.

                    Other example: I've build a CAN router that receives data from one CAN port and routes to another one, depending on the message. Here everything is done in the interrupt routine. Reading into a buffer and processing later is no option.

                    You see, both cases are valid. But Qt's signals/slots are different. If you get a signal, then you are actually in the main. Imagine the slot as a subroutine of main, that is called from Qt's event loop. The interrupt processing has already happened in background, leading to the signal at the next event loop cycle.
                    Of course you should not do blocking operations in a slot, as you block the event loop. Transfer heavy work into a thread, and let the thread raise a signal when it is ready (which is handled in a slot again).

                    This way you get programs that are very clean, easy to understand, and maintain.

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    2
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #10

                      Thanks guys...this has been very helpful. I don't think my application calls for threading (just not enough work), but based on what you've said, I've reorganized things so that the serial reader exits quickly, and the bulk of the work is done in a different object. At least this way, the I/O handler's processing is kept to a minimum.

                      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