I
I
ilyamisyura2015-02-20 16:11:05
OOP
ilyamisyura, 2015-02-20 16:11:05

How to avoid an error when using objects of a class inherited from QObject?

There is a DataSignal class that I inherit from QObject, there is also a Generator class that uses objects of this class, i.e. the generator has a method that must return an object of the DataSignal class. But when compiling, the following error is thrown:
- call to implicitly-deleted copy constructor of 'DataSignal'
- copy constructor of 'DataSignal' is implicitly deleted because base class 'QObject' has a deleted copy constructor

Signal class

#ifndef DATASIGNAL_H
#define DATASIGNAL_H

#include <QObject>
#include <QVector>

class DataSignal : public QObject
{
    Q_OBJECT
public:
    explicit DataSignal(QObject *parent = 0);
    QVector <double> getData();
    QVector <double> getTimeScale();

private:
    QVector <double> data;
    QVector <double> timeScale;

signals:
    void dataChanged(QVector <double> data);
    void timeScaleChanged(QVector <double> data);
    void cleared();

public slots:
    void setData(QVector <double> data);
    void setTimeScale(QVector <double> timeScale);
    void clear();

};

#endif // DATASIGNAL_H

In turn, our generator is inherited from the abstract class
#include <QObject>
#include "datasignal.h"
class AbstractGenerator : public QObject
{
    Q_OBJECT
public:
    explicit AbstractGenerator(QObject *parent = 0);

signals:
    void signalIsGenerated(DataSignal  *signal);

public slots:

};

The generator class itself
#include "abstractgenerator.h"
#include "filtercore.h"

class DiscreteGenerator : public AbstractGenerator
{
public:
    DiscreteGenerator();

public slots:
    DataSignal generateSimpleSignal(/*some params*/);

};

And here, in the *.cpp file, an error occurs:
DataSignal DiscreteGenerator::generateSimpleSignal(/*some params*/){
    DataSignal signal;
    /*something happens*/
    return signal;
}

Help, good people!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vinatorul, 2015-02-20
@ilyamisyura

QObject has blocked copy constructors and assignment operators: Qt Object Model
No copy constructor or assignment operator
The point is that here

DataSignal DiscreteGenerator::generateSimpleSignal(/*some params*/){
    DataSignal signal;
    /*something happens*/
    return signal;
}

You first create a DataSignal object and then return a copy of it.
Try converting to a pointer:
DataSignal* DiscreteGenerator::generateSimpleSignal(/*some params*/){
    DataSignal *signal = new DataSignal;
    /*something happens*/
    return signal;
}

M
me76, 2015-02-20
@me76

You can also explicitly define a DataSignal copy constructor that does not cause a QObject to be copied - this will make it possible to create objects on the stack. But note that such an object must be created without an owner (the QObject *parent parameter in the DataSignal constructor), since the owner considers that the child object is on the heap and tries to delete it in its destructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question