Search code examples
c++qtconstructorcopy-constructorqobject

assignment of two QObject


i have two class names "mamad" and "student" and both of them are inherit from my class "Base" that "Base" inherit from QObject

in Student Class i have a field : "subject" that is a mamad and i have a function (setsubject) that take a newsubject and copy newsubject to subject.

but i have an error :

"QObject& Qobject::operator=(const QObject)" is private !
"within this context" 

how can i fix it?

this is my mamad class :

class mamad:public Base
{
   Q_OBJECT
   Q_PROPERTY(int id2 READ getId2 WRITE setId2)
   Q_PROPERTY(QString Name2 READ getName2 WRITE setName2)
public:
    mamad(Base* parent=0);
    int getId2() const { return id2; }
    void setId2(int newId) { id2 = newId; }
    QString getName2() const { return Name2; }

    void setName2(const QString &newName) { Name2 = newName; }
private:
    int id2;
    QString Name2;

};

and it is my student class:

class student : public Base
{
    Q_OBJECT // Q_OBJECT macro will take care of generating proper metaObject for your class
    Q_PROPERTY(int id READ getId WRITE setId)
    Q_PROPERTY(QString Name READ getName WRITE setName)
    Q_PROPERTY(mamad subject  WRITE setsubject)

public:
    student(Base * parent=0);
    int getId() const { return id; }
    void setId(int newId) { id = newId; }
    QString getName() const { return Name; }
    void setName(const QString &newName) { Name = newName; }
   // mamad getsubject()const {return subject;}
    void setsubject( mamad newsubject) {subject=newsubject; }


private:
    int id;
    QString Name;
    mamad subject;
};

and i must say that i had this problem with getsubject function too , and i don't know how to fix it.?

please , please, help me


Solution

  • It sounds like QObjects are not meant to be copied by default. Perhaps you better write a copy function which copies just exactly what parameters you need from newsubject to subject

    Edit: This post (possible duplicate) expains more, and says you are meant to only store and copy pointers to QObjects and not the objects themselves.

    for e.g - very basic and using raw pointers. Recommended to use std::unique_ptr or std::shared_ptr based on requirement

    class student : public Base
    {
        ...
        void setsubject( mamad* newsubject) {subject=newsubject; }
        ...
        mamad* subject;
    };
    
    stud.setsubject(&mamadObj);