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. Issue in making GUI responsive
Forum Updated to NodeBB v4.3 + New Features

Issue in making GUI responsive

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 6 Posters 4.4k Views 3 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.
  • K Offline
    K Offline
    Kira
    wrote on 18 Jul 2019, 08:49 last edited by
    #1

    Hello All,
    I am working on application where i have to move my device to cetain location on start button click event.
    My problem is when the device is moving my application freezes for time being the device have not moved. So basically i am reading data sequentially waiting for the response data before executuing the next step.
    So the solution for me was to implement the operation in a thread. I have implement thread using QtConcurrent and than i get the following run time error:

    Error:QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QSerialPort(0x20740c92860), parent's thread is QThread(0x20740c47600), current thread is QThread(0x207410864c0)
    So from my understanding QSerialPort is initialized in serialport.cpp file so it cannot be executed from the thread which is invoked in serialport.cpp.
    In my program is i have defined all the serialrelated functions in serialpost.cpp file.

    So i just want to know what elese can be done. And that i what should be the currect approach that i should follw to implement the same

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on 18 Jul 2019, 09:07 last edited by
      #2

      It sounds like your application is overwhelmed by the amount of data generated while moving the device. Reading the data in a separate thread may be a viable solution. However, QtConcurrent does not sound like the right tool for this job.

      QtConcurrent helps you divide work onto multiple cores to speed up processing. What you really need is to keep the UI responsive while data is being processed in the background. This is a different use case for threads, and can be done using QThread.

      Without any code samples or more information on what exactly you are trying to achieve, I find it difficult to give any more specific advice.

      K 1 Reply Last reply 18 Jul 2019, 09:10
      0
      • A Asperamanca
        18 Jul 2019, 09:07

        It sounds like your application is overwhelmed by the amount of data generated while moving the device. Reading the data in a separate thread may be a viable solution. However, QtConcurrent does not sound like the right tool for this job.

        QtConcurrent helps you divide work onto multiple cores to speed up processing. What you really need is to keep the UI responsive while data is being processed in the background. This is a different use case for threads, and can be done using QThread.

        Without any code samples or more information on what exactly you are trying to achieve, I find it difficult to give any more specific advice.

        K Offline
        K Offline
        Kira
        wrote on 18 Jul 2019, 09:10 last edited by
        #3

        @Asperamanca : I will update with code. But even if we use code QThread than also i think:
        Error:QObject: Cannot create children for a parent that is in a different thread. is expected to arise.

        J 1 Reply Last reply 18 Jul 2019, 09:14
        0
        • K Kira
          18 Jul 2019, 09:10

          @Asperamanca : I will update with code. But even if we use code QThread than also i think:
          Error:QObject: Cannot create children for a parent that is in a different thread. is expected to arise.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 18 Jul 2019, 09:14 last edited by
          #4

          @Kira said in Issue in making GUI responsive:

          is expected to arise

          If you use Qt threads in a wrong way, then yes.
          Create your QSerialPort instance in the thread where you want to use it and do not access it anywhere else.

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

          K 1 Reply Last reply 18 Jul 2019, 09:25
          2
          • J jsulm
            18 Jul 2019, 09:14

            @Kira said in Issue in making GUI responsive:

            is expected to arise

            If you use Qt threads in a wrong way, then yes.
            Create your QSerialPort instance in the thread where you want to use it and do not access it anywhere else.

            K Offline
            K Offline
            Kira
            wrote on 18 Jul 2019, 09:25 last edited by
            #5

            @jsulm : Thanks for the reply from the error i was able to understand something like that was happening.
            So i will have to do the following ??

            1. Close the instance of serialPort in mainthread.
            2. Initialize serialport in thread
            3. Do the required operation
              4.Close the serialport in thread and again open it in the mainthread.
            A 1 Reply Last reply 18 Jul 2019, 09:55
            0
            • K Kira
              18 Jul 2019, 09:25

              @jsulm : Thanks for the reply from the error i was able to understand something like that was happening.
              So i will have to do the following ??

              1. Close the instance of serialPort in mainthread.
              2. Initialize serialport in thread
              3. Do the required operation
                4.Close the serialport in thread and again open it in the mainthread.
              A Offline
              A Offline
              Asperamanca
              wrote on 18 Jul 2019, 09:55 last edited by
              #6

              @Kira said in Issue in making GUI responsive:

              @jsulm : Thanks for the reply from the error i was able to understand something like that was happening.
              So i will have to do the following ??

              1. Close the instance of serialPort in mainthread.
              2. Initialize serialport in thread
              3. Do the required operation
                4.Close the serialport in thread and again open it in the mainthread.

              No. Create a thread dedicated to read the serial port. Communicate results with your main thread using signals and slots. Be aware that all communication that way will be asynchronous.

              K 1 Reply Last reply 18 Jul 2019, 10:01
              0
              • A Asperamanca
                18 Jul 2019, 09:55

                @Kira said in Issue in making GUI responsive:

                @jsulm : Thanks for the reply from the error i was able to understand something like that was happening.
                So i will have to do the following ??

                1. Close the instance of serialPort in mainthread.
                2. Initialize serialport in thread
                3. Do the required operation
                  4.Close the serialport in thread and again open it in the mainthread.

                No. Create a thread dedicated to read the serial port. Communicate results with your main thread using signals and slots. Be aware that all communication that way will be asynchronous.

                K Offline
                K Offline
                Kira
                wrote on 18 Jul 2019, 10:01 last edited by
                #7

                @Asperamanca : Actually i have to keep my serialrequest synchronous.

                J 1 Reply Last reply 18 Jul 2019, 10:04
                0
                • K Kira
                  18 Jul 2019, 10:01

                  @Asperamanca : Actually i have to keep my serialrequest synchronous.

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 18 Jul 2019, 10:04 last edited by
                  #8

                  @Kira what part of it, and why?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  K 1 Reply Last reply 18 Jul 2019, 10:42
                  0
                  • J J.Hilk
                    18 Jul 2019, 10:04

                    @Kira what part of it, and why?

                    K Offline
                    K Offline
                    Kira
                    wrote on 18 Jul 2019, 10:42 last edited by
                    #9

                    @J.Hilk : Ok i understand what @Asperamanca is trying to explain. Actually as per the requirement i have to give signal to the controller to move and capture the images at that location and repeat the operation n number of times. I am performing this operation in main thread which is causing main thread to freeze until the controller moves to the location.
                    Using the below codes:

                    serialPort->write(serialCommand.toUtf8());
                      serialPort->waitForReadyRead();
                      //qDebug()<<"Bytes arrived"<<serialPort->bytesAvailable();
                      readData = serialPort->readAll();
                    

                    @J-Hilk : are you suggesting to create a thread which serves the serialrequest to avoid freezing of the mainthread and get the acknowledgement via signal slot mechanism from the thread.
                    As per my current experience with the program it is not feasible to create separate thread as it is called multiple times as it causes a lot of overhead.

                    J J 2 Replies Last reply 18 Jul 2019, 10:46
                    0
                    • K Kira
                      18 Jul 2019, 10:42

                      @J.Hilk : Ok i understand what @Asperamanca is trying to explain. Actually as per the requirement i have to give signal to the controller to move and capture the images at that location and repeat the operation n number of times. I am performing this operation in main thread which is causing main thread to freeze until the controller moves to the location.
                      Using the below codes:

                      serialPort->write(serialCommand.toUtf8());
                        serialPort->waitForReadyRead();
                        //qDebug()<<"Bytes arrived"<<serialPort->bytesAvailable();
                        readData = serialPort->readAll();
                      

                      @J-Hilk : are you suggesting to create a thread which serves the serialrequest to avoid freezing of the mainthread and get the acknowledgement via signal slot mechanism from the thread.
                      As per my current experience with the program it is not feasible to create separate thread as it is called multiple times as it causes a lot of overhead.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 18 Jul 2019, 10:46 last edited by
                      #10

                      @Kira said in Issue in making GUI responsive:

                      serialPort->waitForReadyRead();

                      You're calling this in main thread?!
                      Why don't you use the asynchronous nature of QSerialPort instead?

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

                      1 Reply Last reply
                      0
                      • K Kira
                        18 Jul 2019, 10:42

                        @J.Hilk : Ok i understand what @Asperamanca is trying to explain. Actually as per the requirement i have to give signal to the controller to move and capture the images at that location and repeat the operation n number of times. I am performing this operation in main thread which is causing main thread to freeze until the controller moves to the location.
                        Using the below codes:

                        serialPort->write(serialCommand.toUtf8());
                          serialPort->waitForReadyRead();
                          //qDebug()<<"Bytes arrived"<<serialPort->bytesAvailable();
                          readData = serialPort->readAll();
                        

                        @J-Hilk : are you suggesting to create a thread which serves the serialrequest to avoid freezing of the mainthread and get the acknowledgement via signal slot mechanism from the thread.
                        As per my current experience with the program it is not feasible to create separate thread as it is called multiple times as it causes a lot of overhead.

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 18 Jul 2019, 10:46 last edited by
                        #11

                        @Kira
                        The idea is to create 1 additional thread, probably during application start up.

                        In that your Serialport lives, sending and receiving data and sitting idle when nothing is to be done.

                        Creating a new thread and serialport instance each time you want send/receive data has indeed way too much overhead.

                        I assume you get a message back, when your controller finished moving? You can react to that via a Signal that you emit, to your main thread


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        K 1 Reply Last reply 18 Jul 2019, 10:53
                        3
                        • J J.Hilk
                          18 Jul 2019, 10:46

                          @Kira
                          The idea is to create 1 additional thread, probably during application start up.

                          In that your Serialport lives, sending and receiving data and sitting idle when nothing is to be done.

                          Creating a new thread and serialport instance each time you want send/receive data has indeed way too much overhead.

                          I assume you get a message back, when your controller finished moving? You can react to that via a Signal that you emit, to your main thread

                          K Offline
                          K Offline
                          Kira
                          wrote on 18 Jul 2019, 10:53 last edited by
                          #12

                          @J.Hilk said in Issue in making GUI responsive:

                          I assume you get a message back, when your controller finished moving? You can react to that via a Signal that you emit, to your main thread

                          Ok so this is the approach i should follow?

                          @jsulm said in Issue in making GUI responsive:

                          Why don't you use the asynchronous nature of QSerialPort instead?

                          @jsulm : I have tried that initially but it's not that effective? Basically there are so many request and response that sometimes i don't get response and s.ometimes previous and current responses together which is not feasible

                          J J 2 Replies Last reply 18 Jul 2019, 10:55
                          0
                          • K Kira
                            18 Jul 2019, 10:53

                            @J.Hilk said in Issue in making GUI responsive:

                            I assume you get a message back, when your controller finished moving? You can react to that via a Signal that you emit, to your main thread

                            Ok so this is the approach i should follow?

                            @jsulm said in Issue in making GUI responsive:

                            Why don't you use the asynchronous nature of QSerialPort instead?

                            @jsulm : I have tried that initially but it's not that effective? Basically there are so many request and response that sometimes i don't get response and s.ometimes previous and current responses together which is not feasible

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 18 Jul 2019, 10:55 last edited by
                            #13

                            @Kira said in Issue in making GUI responsive:

                            I have tried that initially but it's not that effective? Basically there are so many request and response that sometimes i don't get response and s.ometimes previous and current responses together which is not feasible

                            Sounds like something is wrong then - I doubt moving QSerialPort to another thread will change this behaviour.

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

                            1 Reply Last reply
                            0
                            • K Kira
                              18 Jul 2019, 10:53

                              @J.Hilk said in Issue in making GUI responsive:

                              I assume you get a message back, when your controller finished moving? You can react to that via a Signal that you emit, to your main thread

                              Ok so this is the approach i should follow?

                              @jsulm said in Issue in making GUI responsive:

                              Why don't you use the asynchronous nature of QSerialPort instead?

                              @jsulm : I have tried that initially but it's not that effective? Basically there are so many request and response that sometimes i don't get response and s.ometimes previous and current responses together which is not feasible

                              J Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 18 Jul 2019, 11:13 last edited by
                              #14

                              @Kira said in Issue in making GUI responsive:

                              Ok so this is the approach i should follow?

                              Imho, yes

                              I have tried that initially but it's not that effective? Basically there are so many request and response that sometimes i don't get response and s.ometimes previous and current responses together which is not feasible

                              Seems to me like you're missing a proper queue implementation on your SerialPort communication.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              K 1 Reply Last reply 18 Jul 2019, 12:02
                              0
                              • J J.Hilk
                                18 Jul 2019, 11:13

                                @Kira said in Issue in making GUI responsive:

                                Ok so this is the approach i should follow?

                                Imho, yes

                                I have tried that initially but it's not that effective? Basically there are so many request and response that sometimes i don't get response and s.ometimes previous and current responses together which is not feasible

                                Seems to me like you're missing a proper queue implementation on your SerialPort communication.

                                K Offline
                                K Offline
                                Kira
                                wrote on 18 Jul 2019, 12:02 last edited by
                                #15

                                @J.Hilk said in Issue in making GUI responsive:

                                Seems to me like you're missing a proper queue implementation on your SerialPort communication.

                                @J-Hilk : I have tried multiple example also with the sample examples that comes with the qt creator. I was having problem with the microcontroller version which i was using.

                                As per what i have understood about signal slot mechanism is that if the slot is not executed at given instance of time it get queued. So their arise a possiblity of the slots getting queue and be the reason of my delay in response creating a bottleneck between the sender and the receiver.

                                J 1 Reply Last reply 18 Jul 2019, 12:08
                                0
                                • K Kira
                                  18 Jul 2019, 12:02

                                  @J.Hilk said in Issue in making GUI responsive:

                                  Seems to me like you're missing a proper queue implementation on your SerialPort communication.

                                  @J-Hilk : I have tried multiple example also with the sample examples that comes with the qt creator. I was having problem with the microcontroller version which i was using.

                                  As per what i have understood about signal slot mechanism is that if the slot is not executed at given instance of time it get queued. So their arise a possiblity of the slots getting queue and be the reason of my delay in response creating a bottleneck between the sender and the receiver.

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 18 Jul 2019, 12:08 last edited by jsulm
                                  #16

                                  @Kira A signal is only queued when communicating between different threads (or setting queued connection type explicitly). In same thread emitting a signal means that all connected slots will be called immediately one after another (in same order as connect() calls), so no queueing.

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

                                  K 1 Reply Last reply 19 Jul 2019, 09:00
                                  1
                                  • J jsulm
                                    18 Jul 2019, 12:08

                                    @Kira A signal is only queued when communicating between different threads (or setting queued connection type explicitly). In same thread emitting a signal means that all connected slots will be called immediately one after another (in same order as connect() calls), so no queueing.

                                    K Offline
                                    K Offline
                                    Kira
                                    wrote on 19 Jul 2019, 09:00 last edited by
                                    #17

                                    @jsulm : @J-Hilk : Guys thanks to you did some sample program regarding implementation of QSerialPort and found an interesting example listed under qt creator.
                                    https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html
                                    Here they have clearly mentioned the example of using serialPort in gui mode.
                                    Just i have one doubt:
                                    =>Here every time port is being set, i don't have any such condition in my example so can just set port once in the thread ?
                                    =>Also does QWaitCondition have any extra overhead ?
                                    =>Can i implement the similar logic for my camera thread where the thread runs continuously without any sleep?

                                    J aha_1980A 2 Replies Last reply 22 Jul 2019, 07:30
                                    0
                                    • K Kira
                                      19 Jul 2019, 09:00

                                      @jsulm : @J-Hilk : Guys thanks to you did some sample program regarding implementation of QSerialPort and found an interesting example listed under qt creator.
                                      https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html
                                      Here they have clearly mentioned the example of using serialPort in gui mode.
                                      Just i have one doubt:
                                      =>Here every time port is being set, i don't have any such condition in my example so can just set port once in the thread ?
                                      =>Also does QWaitCondition have any extra overhead ?
                                      =>Can i implement the similar logic for my camera thread where the thread runs continuously without any sleep?

                                      J Offline
                                      J Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 22 Jul 2019, 07:30 last edited by
                                      #18

                                      @Kira That example shows how to use SYNCHRONOUS API in GUI thread. But why do you want to use synchronous API instead of the asynchronous? If there is no valid reason to use synchronous API use the asynchronous - this is WAY easier compared to dealing with threads.

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

                                      K 1 Reply Last reply 22 Jul 2019, 08:40
                                      4
                                      • K Kira
                                        19 Jul 2019, 09:00

                                        @jsulm : @J-Hilk : Guys thanks to you did some sample program regarding implementation of QSerialPort and found an interesting example listed under qt creator.
                                        https://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html
                                        Here they have clearly mentioned the example of using serialPort in gui mode.
                                        Just i have one doubt:
                                        =>Here every time port is being set, i don't have any such condition in my example so can just set port once in the thread ?
                                        =>Also does QWaitCondition have any extra overhead ?
                                        =>Can i implement the similar logic for my camera thread where the thread runs continuously without any sleep?

                                        aha_1980A Offline
                                        aha_1980A Offline
                                        aha_1980
                                        Lifetime Qt Champion
                                        wrote on 22 Jul 2019, 08:16 last edited by
                                        #19

                                        @Kira and just to add to @jsulm:

                                        I doubt the serial port handling is your problem. Serial ports are slow and handling them with signals and slots is most often no problem.

                                        I think your problem is handling the data you received through serial port. If that takes a bit longer, you'll block your GUI.

                                        Regards

                                        Qt has to stay free or it will die.

                                        K 1 Reply Last reply 22 Jul 2019, 08:45
                                        3
                                        • J jsulm
                                          22 Jul 2019, 07:30

                                          @Kira That example shows how to use SYNCHRONOUS API in GUI thread. But why do you want to use synchronous API instead of the asynchronous? If there is no valid reason to use synchronous API use the asynchronous - this is WAY easier compared to dealing with threads.

                                          K Offline
                                          K Offline
                                          Kira
                                          wrote on 22 Jul 2019, 08:40 last edited by Kira
                                          #20

                                          @jsulm said in Issue in making GUI responsive:

                                          That example shows how to use SYNCHRONOUS API in GUI thread.

                                          Will this approach cause my GUI to hang if i don't get a timely response??

                                          Can you please suggest how can i implement the following requirement using asynchronous approch?
                                          ->Send serial data
                                          ->Read the data
                                          ->If data read successfully than allow another serial request.

                                          J 1 Reply Last reply 22 Jul 2019, 10:07
                                          0

                                          1/28

                                          18 Jul 2019, 08:49

                                          • Login

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