Answer the question
In order to leave comments, you need to log in
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
#include <QObject>
#include "datasignal.h"
class AbstractGenerator : public QObject
{
Q_OBJECT
public:
explicit AbstractGenerator(QObject *parent = 0);
signals:
void signalIsGenerated(DataSignal *signal);
public slots:
};
#include "abstractgenerator.h"
#include "filtercore.h"
class DiscreteGenerator : public AbstractGenerator
{
public:
DiscreteGenerator();
public slots:
DataSignal generateSimpleSignal(/*some params*/);
};
DataSignal DiscreteGenerator::generateSimpleSignal(/*some params*/){
DataSignal signal;
/*something happens*/
return signal;
}
Answer the question
In order to leave comments, you need to log in
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;
}
DataSignal* DiscreteGenerator::generateSimpleSignal(/*some params*/){
DataSignal *signal = new DataSignal;
/*something happens*/
return signal;
}
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 questionAsk a Question
731 491 924 answers to any question