View Full Version : ارث بری از Qobject
returnx
سه شنبه 30 مهر 1392, 18:44 عصر
کلاس داریم که می خواهیم از Qobject ارث بری کنیم اما با این خطا مواجه میشیم :
error: 'QObject::QObject(const QObject&)' is private
یعنی چی تابع سازنده Qobject
Private تعریف شده!؟
omidshaman
سه شنبه 30 مهر 1392, 19:47 عصر
QObjec این Copy constructor رو ساپورت نمی کنه اصلا
متن داکیونت Qt
No copy constructor or assignment operator
QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macroQ_DISABLE_COPY (qthelp://org.qt-project.qtcore.511/qtcore/qobject.html#Q_DISABLE_COPY)(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value (qthelp://org.qt-project.qtcore.511/qtcore/object.html#identity-vs-value) on the Qt Object Model (qthelp://org.qt-project.qtcore.511/qtcore/object.html) page.
The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers
Qt Objects: Identity vs Value
Some of the added features listed above for the Qt Object Model, require that we think of Qt Objects as identities, not values. Values are copied or assigned; identities are cloned. Cloning means to create a new identity, not an exact copy of the old one. For example, twins have different identities. They may look identical, but they have different names, different locations, and may have completely different social networks.
Then cloning an identity is a more complex operation than copying or assigning a value. We can see what this means in the Qt Object Model.
A Qt Object...
might have a unique QObject::objectName (qthelp://org.qt-project.qtcore.511/qtcore/qobject.html#objectName-prop)(). If we copy a Qt Object, what name should we give the copy?
has a location in an object hierarchy (qthelp://org.qt-project.qtcore.511/qtcore/objecttrees.html). If we copy a Qt Object, where should the copy be located?
can be connected to other Qt Objects to emit signals to them or to receive signals emitted by them. If we copy a Qt Object, how should we transfer these connections to the copy?
can have new properties (qthelp://org.qt-project.qtcore.511/qtcore/properties.html#qt-s-property-system) added to it at runtime that are not declared in the C++ class. If we copy a Qt Object, should the copy include the properties that were added to the original?
For these reasons, Qt Objects should be treated as identities, not as values. Identities are cloned, not copied or assigned, and cloning an identity is a more complex operation than copying or assigning a value. Therefore, QObject (qthelp://org.qt-project.qtcore.511/qtcore/qobject.html) and all subclasses of QObject (qthelp://org.qt-project.qtcore.511/qtcore/qobject.html) (direct or indirect) have their copy constructor and assignment operator (qthelp://org.qt-project.qtcore.511/qtcore/qobject.html#no-copy-constructor) disabled.
حامد مصافی
سه شنبه 30 مهر 1392, 22:13 عصر
تعریف سازنده رو به فایل cpp انتقال بده
omidshaman
چهارشنبه 01 آبان 1392, 00:39 صبح
ربطی به تعریف سازنده نداره اون ارور اصلا مال سازنده نیست مربوط به copy consructor ه من جایی ندیدم گزاشتن تعریف توی فایل .h مشکل ایجاد کنه !
وقتایی این مشکل پیش میاد که بخوای مثلا یک آبجکت ساخته شده که از QObject ارث برده رو مثلا به یک وکتور append کنی یا چیزای دیگه .
همون جوری که توی داکیومنت گفته شده باید از پوینتر استفاده کنی .
مثلا
//ارور
ItemInheritFromObject item;
std::vector<ItemInheritFromObject>testList;
testList.push_back(item);
//ارور
ItemInheritFromObject w,w2;
w=w2;
//درست
ItemInheritFromObject item;
std::vector<ItemInheritFromObject*>testList;
testList.push_back(&item);
returnx
چهارشنبه 01 آبان 1392, 15:56 عصر
ربطی به تعریف سازنده نداره اون ارور اصلا مال سازنده نیست مربوط به copy consructor ه من جایی ندیدم گزاشتن تعریف توی فایل .h مشکل ایجاد کنه !
وقتایی این مشکل پیش میاد که بخوای مثلا یک آبجکت ساخته شده که از QObject ارث برده رو مثلا به یک وکتور append کنی یا چیزای دیگه .
همون جوری که توی داکیومنت گفته شده باید از پوینتر استفاده کنی .
مثلا
//ارور
ItemInheritFromObject item;
std::vector<ItemInheritFromObject>testList;
testList.push_back(item);
//ارور
ItemInheritFromObject w,w2;
w=w2;
//درست
ItemInheritFromObject item;
std::vector<ItemInheritFromObject*>testList;
testList.push_back(&item);
من اصلا هیچ شی از این کلاس نمیسازم ، اما باز هم وقتی کامپایل می کنم خطا میگیره!!
الان این کلاس هایی که به صورت مستقیم از Qobject مشتق شده اند ، به چه صورت پیاده شده اند!؟
حامد مصافی
چهارشنبه 01 آبان 1392, 16:08 عصر
کد رو بذار اینجا ببینیم
returnx
چهارشنبه 01 آبان 1392, 16:16 عصر
نه من اشتباه کردم ، در یک فایل هدر دیگه در یک تابع از این کلاس یک شی ساخته میشد وقتی با اشاره گر شی را ساختم خطا بر طرف شد ، اما من دقیقا دلیل خطا را متوجه نشدم!؟
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.