-1

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

Erfan Tavakoli
  • 336
  • 1
  • 6
  • 14

2 Answers2

1

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);
Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • thank you for your answer , but can you tell me an example for that (pointers) ? Merci . – Erfan Tavakoli Dec 13 '12 at 05:15
  • @ErfanTavakoli I have no idea of qt itself, but i have added a basic example of using it with [pointers](http://www.cplusplus.com/doc/tutorial/pointers/). – Karthik T Dec 13 '12 at 05:27
1

QObject can not be copied as it says clearly in docs and in the error message.

You will have to use setsubject as a clone method i.e. duplicate the state by manually copying the fields between the mamads.

cmannett85
  • 21,725
  • 8
  • 76
  • 119