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. QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...
Forum Updated to NodeBB v4.3 + New Features

QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 5 Posters 3.4k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Hi,

    Why is your slot getting a copy of Data while the other method uses a const reference ?

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

    O 3 Replies Last reply
    2
    • SGaistS SGaist

      Hi,

      Why is your slot getting a copy of Data while the other method uses a const reference ?

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

      @SGaist LONG WINAPI CallBack(const Data &data) this is provided by 3rdParty and it's not in MainThread, so i pass-by-value across threads to MainThread, and write the slot: void slotUpdate(Data data)

      1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Why is your slot getting a copy of Data while the other method uses a const reference ?

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

        @SGaist type error, have editted.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Why is your slot getting a copy of Data while the other method uses a const reference ?

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

          @SGaist i have to pass-by-value when multi-threads, right?any better way?

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

            @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

            @SGaist i have to pass-by-value when multi-threads, right?any better way?

            That formulation is way too generic. With Qt signals and slots, the internal mechanism does a copy before sending the data to the other thread.

            Also, I didn't wrote about your callback, I wrote about the slotUpdate slot.

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

            O 1 Reply Last reply
            0
            • SGaistS SGaist

              @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

              @SGaist i have to pass-by-value when multi-threads, right?any better way?

              That formulation is way too generic. With Qt signals and slots, the internal mechanism does a copy before sending the data to the other thread.

              Also, I didn't wrote about your callback, I wrote about the slotUpdate slot.

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

              @SGaist
              bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));
              public slots:
              void slotUpdate(Data data)

              pass-by-value should write like the above, right? if so, i donot know why it called twice the: Data::Data(const Data& other)

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

                The first copy comes from the fact that you are using a queued connection which means that the data passed to it must be copied.

                Then your slotUpdate method pass data by value which means that it is a copy again. Hence the two copies.

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

                O 1 Reply Last reply
                4
                • SGaistS SGaist

                  The first copy comes from the fact that you are using a queued connection which means that the data passed to it must be copied.

                  Then your slotUpdate method pass data by value which means that it is a copy again. Hence the two copies.

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

                  @SGaist can i make it just 1-time copy?
                  bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));
                  public slots:
                  void slotUpdate(const Data& data)//is this ok???

                  jsulmJ 1 Reply Last reply
                  0
                  • O opengpu

                    @SGaist can i make it just 1-time copy?
                    bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));
                    public slots:
                    void slotUpdate(const Data& data)//is this ok???

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

                    @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                    void slotUpdate(const Data& data)//is this ok???

                    Yes it is and this is what @SGaist already suggested...

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

                    O 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                      void slotUpdate(const Data& data)//is this ok???

                      Yes it is and this is what @SGaist already suggested...

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

                      @jsulm cool ! so i can always pass-by-ref in Slot, whether it's in one-thread or two-thread?
                      is there anything should be careful when signal and slot are not in the same thread, and the param is not basic type but custom Stuct/Class?

                      jsulmJ J.HilkJ 2 Replies Last reply
                      0
                      • O opengpu

                        @jsulm cool ! so i can always pass-by-ref in Slot, whether it's in one-thread or two-thread?
                        is there anything should be careful when signal and slot are not in the same thread, and the param is not basic type but custom Stuct/Class?

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

                        @opengpu As long as a copy is passed between the threads you do not have to worry. But be careful if your structs contain pointers to other data, or if you're passing pointers!

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

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

                          class DataWidget : public QWidget
                          {
                          Q_OBJECT
                          public slots:
                          void slotUpdate(const Data& data)//changed to ref
                          {
                          update(data);
                          }

                          However, i found these 2 style, by Data or const Data&, they both work, and call Data::Data(const Data& other) 1 time.
                          bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));//why this works while the slot is const Data&
                          bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(const Data&, data));

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

                            another question, i found:
                            //Q_DECLARE_METATYPE(Data);//i can delete this line, and only with qRegisterMetaType that will still work...

                            //int id = qRegisterMetaType<Data>("Data");
                            //qRegisterMetaType<Data>("Data");//which should i use, as i pass const Data& in slotUpdate...
                            qRegisterMetaType<Data>("const Data&");//which should i use, as i pass const Data& in slotUpdate...
                            
                            1 Reply Last reply
                            0
                            • O opengpu

                              class DataWidget : public QWidget
                              {
                              Q_OBJECT
                              public slots:
                              void slotUpdate(const Data& data)//changed to ref
                              {
                              update(data);
                              }

                              However, i found these 2 style, by Data or const Data&, they both work, and call Data::Data(const Data& other) 1 time.
                              bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));//why this works while the slot is const Data&
                              bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(const Data&, data));

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

                              @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                              why this works while the slot is const

                              Why should it not? data will be const in the called slot. It doesn't matter whether it is const or not in the caller.

                              There is no need to register "const Data&". If Data is registered everything else (const, &, *, ...) is simply an "attribute" for an already known type. It is like with the compiler: if the compiler knows what Data is it knows what const Data& is.

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

                              O 1 Reply Last reply
                              0
                              • O opengpu

                                @jsulm cool ! so i can always pass-by-ref in Slot, whether it's in one-thread or two-thread?
                                is there anything should be careful when signal and slot are not in the same thread, and the param is not basic type but custom Stuct/Class?

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

                                @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                                so i can always pass-by-ref in Slot, whether it's in one-thread or two-thread

                                be careful that you do not explicitly define the connection as a DirectConnection. I'm not 100% sure, but I think then it will be passed by reference even across threads.


                                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 said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                                  so i can always pass-by-ref in Slot, whether it's in one-thread or two-thread

                                  be careful that you do not explicitly define the connection as a DirectConnection. I'm not 100% sure, but I think then it will be passed by reference even across threads.

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

                                  @J.Hilk said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                                  e careful that

                                  actually even i use "const Data&" as slot's param, it's still safe while signal and slot are in different slots.
                                  Data::Data(const Data& other) is called 1 time in invokeMethod
                                  bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(const Data&, data));

                                  1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                                    why this works while the slot is const

                                    Why should it not? data will be const in the called slot. It doesn't matter whether it is const or not in the caller.

                                    There is no need to register "const Data&". If Data is registered everything else (const, &, *, ...) is simply an "attribute" for an already known type. It is like with the compiler: if the compiler knows what Data is it knows what const Data& is.

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

                                    @jsulm "const Data&", i think normally it's not safe to pass-by-reference across threads, right?

                                    VRoninV jsulmJ 2 Replies Last reply
                                    0
                                    • O opengpu

                                      @jsulm "const Data&", i think normally it's not safe to pass-by-reference across threads, right?

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #22

                                      @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                                      i think normally it's not safe to pass-by-reference across threads, right?

                                      Depends who you ask. The chair of the C++ standard committee is of the opinion that as STL uses const as implying thread safety* the entire language should conform to that convention. In that case using cons& as argument is safe even across threads. On the other hand it's a jungle out there and it's very easy to break that convention so I would not bet my life on thread safety of const references

                                      *source: https://herbsutter.com/2013/01/01/video-you-dont-know-const-and-mutable/

                                      "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
                                      1
                                      • O opengpu

                                        @jsulm "const Data&", i think normally it's not safe to pass-by-reference across threads, right?

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

                                        @opengpu said in QMetaObject::invokeMethod pass custom-type called Twice custom-type's copy constructor...:

                                        it's not safe to pass-by-reference across threads, right?

                                        @SGaist already explained in this thread that the parameter is copied when using queued connection between threads, so it IS safe...

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

                                        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