Signal slot connection does not work with Move Semantics



  • 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?



  • Hi @Kofr

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

    -Michael.



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



  • @m.sue the question is why?



  • @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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.