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. Signal slot connection does not work with Move Semantics
Qt 6.11 is out! See what's new in the release blog

Signal slot connection does not work with Move Semantics

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.4k 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by
    #1

    Here I present simple code, which represents a problem

    #include <QCoreApplication>
    #include <QList>
    
    struct Data {
        int data;
    };
    
    class Obj: public QObject {
        Q_OBJECT
    public:
        Obj() : QObject(nullptr) {
            connect(this, &Obj::startMove, this, &Obj::receiveData);
            emitMoveOp();
        }
        void emitMoveOp() {
            QList<Data> data;
            data.append({1});
            emit startMove(std::move(data));
        }
    
    
    public slots:
        void receiveData(QList<Data> && data) {
            dataList = data;
        }
    signals:
        void startMove(QList<Data> && data);
    private:
        QList<Data> dataList;
    
    };
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Obj obj;
        return a.exec();
    }
    

    I am getting an error caused by some problem with connection D:\dev\tools\Qt\5.9.1\mingw53_32\include\QtCore\qobjectdefs_impl.h:136: error: forming pointer to reference type 'QtPrivate::RemoveRef<QList<Data>&&>::Type {aka QList<Data>&&}' (o->*f)((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]); ^
    How to make signal slot connection work with rvalues?

    m.sueM 1 Reply Last reply
    0
    • K Kofr

      Here I present simple code, which represents a problem

      #include <QCoreApplication>
      #include <QList>
      
      struct Data {
          int data;
      };
      
      class Obj: public QObject {
          Q_OBJECT
      public:
          Obj() : QObject(nullptr) {
              connect(this, &Obj::startMove, this, &Obj::receiveData);
              emitMoveOp();
          }
          void emitMoveOp() {
              QList<Data> data;
              data.append({1});
              emit startMove(std::move(data));
          }
      
      
      public slots:
          void receiveData(QList<Data> && data) {
              dataList = data;
          }
      signals:
          void startMove(QList<Data> && data);
      private:
          QList<Data> dataList;
      
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          Obj obj;
          return a.exec();
      }
      

      I am getting an error caused by some problem with connection D:\dev\tools\Qt\5.9.1\mingw53_32\include\QtCore\qobjectdefs_impl.h:136: error: forming pointer to reference type 'QtPrivate::RemoveRef<QList<Data>&&>::Type {aka QList<Data>&&}' (o->*f)((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]); ^
      How to make signal slot connection work with rvalues?

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi @Kofr

      I would guess that move semantics is not supported by the signal slot mechanism.

      -Michael.

      K 1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        QList uses shared data, there is basically no advantage in using move semantic. Switch to the good old const QList<Data>& data

        "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
        2
        • m.sueM m.sue

          Hi @Kofr

          I would guess that move semantics is not supported by the signal slot mechanism.

          -Michael.

          K Offline
          K Offline
          Kofr
          wrote on last edited by
          #4

          @m.sue the question is why?

          VRoninV 1 Reply Last reply
          0
          • K Kofr

            @m.sue the question is why?

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

            @Kofr said in Signal slot connection does not work with Move Semantics:

            the question is why?

            if you check the moc output it's pretty clear. Arguments are "stored" as void* and then casted to the correct type (QList<Data>) and passed to the slot that can't receive it as it only accepts QList<Data>&&

            See https://bugreports.qt.io/browse/QTBUG-60339

            "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

            m.sueM 1 Reply Last reply
            2
            • VRoninV VRonin

              @Kofr said in Signal slot connection does not work with Move Semantics:

              the question is why?

              if you check the moc output it's pretty clear. Arguments are "stored" as void* and then casted to the correct type (QList<Data>) and passed to the slot that can't receive it as it only accepts QList<Data>&&

              See https://bugreports.qt.io/browse/QTBUG-60339

              m.sueM Offline
              m.sueM Offline
              m.sue
              wrote on last edited by m.sue
              #6

              @VRonin
              @Kofr

              Yeah. The gerrit commits listed in the bug report you quoted show that move semantics are already implemented and even used in the development branch of Qt: https://codereview.qt-project.org/#/c/192417/6/src/widgets/widgets/qmenu.h

              So we will not have to wait for long.

              -Michael.

              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