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. what is the best way to update UI by data(got from the non-mainThread)?
Qt 6.11 is out! See what's new in the release blog

what is the best way to update UI by data(got from the non-mainThread)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 4 Posters 2.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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #2

    which method is more efficient?
    1st way should add mutex;
    2nd way is pass by value;

    JonBJ jsulmJ 2 Replies Last reply
    0
    • O opengpu

      what is the best way to update UI by data(got from the non-mainThread)?
      the data type is not basic, but a struct.

      i know 2 ways:

      1. write a thread-safe class with SetData & GetData
      2. pass the Struct as param by connect(), from sender(noo-mainThread) to reciever(mainThread). i know it can be done by use : Q_DECLARE_METATYPE, qRegisterMetaType
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #3

      @opengpu
      Whatever the data, if it's in a non-GUI thread you will want to define a signal to send it to the GUI thread and a slot in the GUI thread to retrieve it and do whatever to the GUI.

      You'll want to think about the connection type. See https://doc.qt.io/qt-5/threads-qobject.html, Signals and Slots Across Threads. And you'll want to think about the lifetime/ownership of the data structure you're passing, since it's across threads.

      1 Reply Last reply
      3
      • O opengpu

        which method is more efficient?
        1st way should add mutex;
        2nd way is pass by value;

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @opengpu

        which method is more efficient?
        1st way should add mutex;

        As a general statement: using a mutex across threads will cause synchronous behaviour: one thread gets blocked while the other reads/writes. Using signals/slots will, at least in theory, allow the scheduler to decide when best to allow each thread to run independently, without blocking. Whether this makes much difference in practice with all the various overheads going on is a different matter.

        1 Reply Last reply
        1
        • O opengpu

          which method is more efficient?
          1st way should add mutex;
          2nd way is pass by value;

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

          @opengpu I suggest to use signals/slots as this is tested and proven concept and you do not have to do anything special. Synchronising threads manually can be tricky and error prone task.

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

          O 1 Reply Last reply
          3
          • jsulmJ jsulm

            @opengpu I suggest to use signals/slots as this is tested and proven concept and you do not have to do anything special. Synchronising threads manually can be tricky and error prone task.

            O Offline
            O Offline
            opengpu
            wrote on last edited by
            #6

            @jsulm thank you .
            and how is the speed, as i have to emit a not small Struct across threads

            jsulmJ 1 Reply Last reply
            0
            • O opengpu

              @jsulm thank you .
              and how is the speed, as i have to emit a not small Struct across threads

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

              @opengpu Depends on the size of the struct and how often you emit the signal

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

              1 Reply Last reply
              4
              • O Offline
                O Offline
                opengpu
                wrote on last edited by
                #8

                emit very frequently, but the struct is not very big

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  opengpu
                  wrote on last edited by
                  #9

                  is custom Struct used as param of connect effiecnt?

                  J.HilkJ 1 Reply Last reply
                  0
                  • O opengpu

                    is custom Struct used as param of connect effiecnt?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #10

                    @opengpu said in what is the best way to update UI by data(got from the non-mainThread)?:

                    is custom Struct used as param of connect effiecnt?

                    totally depends on the efficiency of your copy constructor of your struct


                    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.

                    O 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @opengpu said in what is the best way to update UI by data(got from the non-mainThread)?:

                      is custom Struct used as param of connect effiecnt?

                      totally depends on the efficiency of your copy constructor of your struct

                      O Offline
                      O Offline
                      opengpu
                      wrote on last edited by
                      #11

                      @J.Hilk thank you.
                      but as signal and slot are not in the same thread. is there any other way except these 2 method?

                      1. pass Struct as value;
                      2. SetData() {mutex; m_data = data;} @the signal thread
                        GetData() {mutex; return m_data;} @the slot thread
                      jsulmJ 1 Reply Last reply
                      0
                      • O opengpu

                        @J.Hilk thank you.
                        but as signal and slot are not in the same thread. is there any other way except these 2 method?

                        1. pass Struct as value;
                        2. SetData() {mutex; m_data = data;} @the signal thread
                          GetData() {mutex; return m_data;} @the slot thread
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @opengpu How big is your struct and how often do you emit the signal?

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

                        O 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @opengpu How big is your struct and how often do you emit the signal?

                          O Offline
                          O Offline
                          opengpu
                          wrote on last edited by opengpu
                          #13

                          @jsulm ```
                          class Data
                          {
                          public:
                          VARIANT data1;
                          VARIANT data11;
                          VARIANT data111;
                          VARIANT data1111;
                          LONG errorCode;
                          public:
                          Data();
                          ~Data();
                          };

                          
                          maybe emit every second, or even more frequently
                          jsulmJ 1 Reply Last reply
                          0
                          • O Offline
                            O Offline
                            opengpu
                            wrote on last edited by opengpu
                            #14

                            is this a good way to emit signal in the non-thread non-qt Callback function?

                            WINAPI Callback(const Data& data)
                            {//here is not mainThread; so i have to pass the data by value to the slot which is in mainThread; or i use mutex & save data to m_data, and then in slot mainThread getData also with mutex protect thread-safety
                                g_qtObj->emit mySignal();
                            }
                            
                            class QtObj : public QObject
                            {
                            Q_OBJECT
                            signals:
                                void mySignal();
                            }
                            QtObj g_obj;
                            QtObj* g_qtObj = &g_obj;
                            
                            1 Reply Last reply
                            0
                            • O opengpu

                              @jsulm ```
                              class Data
                              {
                              public:
                              VARIANT data1;
                              VARIANT data11;
                              VARIANT data111;
                              VARIANT data1111;
                              LONG errorCode;
                              public:
                              Data();
                              ~Data();
                              };

                              
                              maybe emit every second, or even more frequently
                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @opengpu And how big is VARIANT?
                              1HZ isn't something I would care. In such a case I would exchange the data via signals/slots between the threads instead of messing up with threads synchronisation.

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

                              O 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @opengpu And how big is VARIANT?
                                1HZ isn't something I would care. In such a case I would exchange the data via signals/slots between the threads instead of messing up with threads synchronisation.

                                O Offline
                                O Offline
                                opengpu
                                wrote on last edited by
                                #16

                                @jsulm
                                https://docs.microsoft.com/en-us/windows/desktop/winauto/variant-structure
                                https://docs.microsoft.com/en-us/windows/desktop/api/oaidl/ns-oaidl-tagvariant

                                J.HilkJ 1 Reply Last reply
                                0
                                • O opengpu

                                  @jsulm
                                  https://docs.microsoft.com/en-us/windows/desktop/winauto/variant-structure
                                  https://docs.microsoft.com/en-us/windows/desktop/api/oaidl/ns-oaidl-tagvariant

                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #17

                                  @opengpu doesn't really answer @jsulm question, as WORD can be 16, 32, or 64 bits (go Microsoft) x)

                                  even worst case, the size of your struct isn't big at all and any repeating signal with an interval > 100ms is (usually)more than fine


                                  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.

                                  O 1 Reply Last reply
                                  0
                                  • J.HilkJ J.Hilk

                                    @opengpu doesn't really answer @jsulm question, as WORD can be 16, 32, or 64 bits (go Microsoft) x)

                                    even worst case, the size of your struct isn't big at all and any repeating signal with an interval > 100ms is (usually)more than fine

                                    O Offline
                                    O Offline
                                    opengpu
                                    wrote on last edited by
                                    #18

                                    @J.Hilk windows10 & x64

                                    1 Reply Last reply
                                    0
                                    • O Offline
                                      O Offline
                                      opengpu
                                      wrote on last edited by
                                      #19

                                      ok, so i will pass it by-value in connect & across theads

                                      1 Reply Last reply
                                      0
                                      • O Offline
                                        O Offline
                                        opengpu
                                        wrote on last edited by
                                        #20

                                        my exe recieve data from internet then update and show the data realtime, such as stock market realtime price changing very fast.

                                        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