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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by VRonin
    #1

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

    //------------------------------------------------
    1st time: (this is all in non-mainThread)
    QMetaObject::invokeMethod(g_pMainWin, "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));
    ////
    namespace QtMetaTypePrivate {
    template <typename T, bool Accepted = true>
    struct QMetaTypeFunctionHelper {
        static void *Construct(void *where, const void *t)
        {
            if (t)
                return new (where) T(*static_cast<const T*>(t));
    ////
    Data::Data(const Data& other)
    {
    
    //------------------------------------------------
    2nd time:(this is all in mainThread)
    void DataWidget::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
    {
    if (_c == QMetaObject::InvokeMetaMethod) {
    auto *_t = static_cast<DataWidget *>(_o);
    Q_UNUSED(_t)
    switch (_id) {
    case 0: _t->slotUpdate((reinterpret_cast< Data()>(_a[1]))); break;
    ////
    Data::Data(const Data& other)
    {
    
    1 Reply Last reply
    0
    • O Offline
      O Offline
      opengpu
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • O Offline
        O Offline
        opengpu
        wrote on last edited by VRonin
        #3
        Q_DECLARE_METATYPE(Data);
        //int id = qRegisterMetaType<Data>("Data");
        LONG WINAPI CallBack(const Data &data)
        {//non-mainThread
        	assert(g_pMainWin && g_pMainWin ->GetDataWidget());
        	bool b = QMetaObject::invokeMethod(g_pMainWin ->GetDataWidget(), "slotUpdate", Qt::QueuedConnection, Q_ARG(Data, data));
        	assert(b);
        	return 0;
        }
        
        1 Reply Last reply
        0
        • O Offline
          O Offline
          opengpu
          wrote on last edited by opengpu
          #4
          class DataWidget : public QWidget
          {
          	Q_OBJECT
          public slots:
              void slotUpdate(Data data)
              {
          	update(data);
              }
          protected:
              void DataWidget::update(const Data& d)
              {//update UI by d
              }
          
          1 Reply Last reply
          0
          • 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

                                          • Login

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