简体   繁体   中英

Use of deleted function error in qt C++ signals and slots

Could anyone please help me why I am getting this deleted function error even though I am passing parameters as pointer references (*&) ? This is a simple project working with multithreading in Qt, I just want to experiment sharing and modifying objects between parent and child threads. I am new in Qt, I am using Qt 5.9.2 with GCC 4.9.4 on Ubuntu Linux 14.04.

Thanks in advance.

===== Controller.hpp ===========
#pragma once

#include "data.hpp"

#include <QObject>
#include <QThread>

class Controller : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    explicit Controller(QObject *parent = nullptr);
    ~Controller();

signals:
    void operate(Data*& data);

public slots:
    void handleResults(int idx);

private:
    Data _data;
};

===== Controller.cpp ===========
#include "controller.hpp"
#include "worker.hpp"

#include <QDebug>

Controller::Controller(QObject *parent) : QObject(parent)
{
    Worker *worker = new Worker;
    worker->moveToThread(&workerThread);
    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
    connect(this, &Controller::operate, worker, &Worker::doWork);
    connect(worker, &Worker::resultReady, this, &Controller::handleResults);
    workerThread.start();

    emit operate(&_data);
}

Controller::~Controller()
{
    workerThread.quit();
    workerThread.wait();
}

void Controller::handleResults(int idx)
{
    qDebug() << _data.at(idx);
}

===== Worker.hpp ===========
#pragma once

#include "data.hpp"

#include <QObject>

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr);

signals:
    void resultReady(int idx);

public slots:
    void doWork(Data*& data);
};

===== Worker.cpp ===========
#include "worker.hpp"

Worker::Worker(QObject *parent) : QObject(parent)
{
}

void Worker::doWork(Data*& data)
{
    data->insertVal(10);
    emit resultReady(data->getSize());
}

===== Data.hpp ===========
#pragma once

#include <QObject>
#include <QVector>

class Data : public QObject
{
    Q_OBJECT
public:
    explicit Data(QObject *parent = nullptr);

    void insertVal(int i);
    int getSize() const { return _intVec.size(); }

private:
    QVector<int*> _intVec;
};

===== Data.cpp ===========
#include <QDebug>

#include "data.hpp"

Data::Data(QObject *parent) : QObject(parent)
{
}

void Data::insertVal(int i)
{
    _intVec << &i;
    qDebug() << "Data inserted: " << i;
}

===== main.cpp ===========
#include "controller.hpp"
#include "data.hpp"

#include <QCoreApplication>

int main(int argc, char *argv[])
{
    qRegisterMetaType<Data>("Data");

    QCoreApplication a(argc, argv);

    Controller con;
    return a.exec();
}

I am getting these errors:

/home/raze/Qt/5.9.2/gcc_64/include/QtCore/qmetatype.h:765: error: use of deleted function ‘Data::Data(const Data&)’
             return new (where) T(*static_cast<const T*>(t));

/home/raze/Qt/5.9.2/gcc_64/include/QtCore/qobject.h:454: error: ‘QObject::QObject(const QObject&)’ is private
     Q_DISABLE_COPY(QObject)
                    ^
                                                           ^
/home/raze/Documents/Qt_projects/PracThread/data.hpp:6: error: within this context
 class Data : public QObject
       ^

/home/raze/Documents/Qt_projects/PracThread/data.hpp:6: error: use of deleted function ‘QObject::QObject(const QObject&)’

if you change void operate(Data*& data); to void operate(Data* data); in Controller class and change void doWork(Data*& data); to void doWork(Data* data); in worker class your code compiles well But from my comment Read How Qt Signals and Slots Work and Effective Threading Using Qt this articles looks good for all new to Qt/C++.

Edit:

from @altaf comments : To clarify, the root of the problem was cause by qRegisterMetaType<Data>("Data"); in my main.cpp file. Data should be passed by pointers through signals and slots, so it should be registered as, something like qRegisterMetaType<*Data>("pData"); All signals and slots should take the arguments as Data* pData

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM