Qt inheritance problem
-
Hello everybody,
I have a QWidget child class, and I wrote another class which include QObject, as below,
Page Class :
#include <QObject> class Page { public: Page(); ~Page(); private: QString PageTitle; signals: public slots: };Child page inherit the Page
#include <QWidget> #include <QTabWidget> #include "page.h" class ImageManagerPage : public QWidget, public Page { Q_OBJECT public: explicit ImageManagerPage(QWidget *parent = 0); ~ImageManagerPage(); private: signals: public slots: };And the problem is that,
- I'm not sure is this the right way to inherit a class??
- I can't use the QString PageTitle which declare in the Page as private value. like below :
ImageManagerPage::ImageManagerPage(QWidget *parent) : QWidget(parent),Page() { this->PageTitle = "Image Manager"; }The ERROR msg is :
page.h:14: error: 'QString Page::PageTitle' is private
QString PageTitle;Anyone familiar with Qt inheritance ?? I 'm not share the different between C++ which I used in VS.
THANKS~!! -
Hi @Phong
In oriented object programming , you need to know about encapsulation
private - Only the current class will have access to the field or method. protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method. public - Any class can refer to the field or call the method. This assumes these keywords are used as part of a field or method declaration within a class definition.You PageTitle member variable should be at least protected.
One last thing:
I noticed that you have signal, and slot section on your page class:
signals:
public slots:
You need to know that :
All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.Refer to the doc from this link:
http://doc.qt.io/qt-4.8/signalsandslots.html
Hope this can help!
-
Hi @Phong
In oriented object programming , you need to know about encapsulation
private - Only the current class will have access to the field or method. protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method. public - Any class can refer to the field or call the method. This assumes these keywords are used as part of a field or method declaration within a class definition.You PageTitle member variable should be at least protected.
One last thing:
I noticed that you have signal, and slot section on your page class:
signals:
public slots:
You need to know that :
All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.Refer to the doc from this link:
http://doc.qt.io/qt-4.8/signalsandslots.html
Hope this can help!
@mostefa said in Qt inheritance problem:
They mu
Hi @mostefa ,
I got the point, thanks!!
But there is another problem that's,
I tried it before to inherit QObject to Page class,
and inherit both QWidget and Page will reach to a QObject Ambiguous issue.
Anything I did it wrong?? -
@mostefa said in Qt inheritance problem:
They mu
Hi @mostefa ,
I got the point, thanks!!
But there is another problem that's,
I tried it before to inherit QObject to Page class,
and inherit both QWidget and Page will reach to a QObject Ambiguous issue.
Anything I did it wrong??@Phong said in Qt inheritance problem:
@mostefa said in Qt inheritance problem:
They mu
Hi @mostefa ,
I got the point, thanks!!
But there is another problem that's,
I tried it before to inherit QObject to Page class,
and inherit both QWidget and Page will reach to a QObject Ambiguous issue.
Anything I did it wrong??i do not think you have to use signal and slot at page class , so you do not need to inherit from
QObjectat all,this will avoid conflict, cause Ifyou will inherit from
QObjecton your page class
than inherit fromQWidget, page on yourImageManagerPageThis will lead to a kind of conflict:
ImageManagerPage:
1)Inherit from QWidget ,and QWidget inherit from QObject
2)Inherit from Page , and page inherit from QObjectSo there is something not clear here , please refere to the doc from this link:
http://doc.qt.io/qt-4.8/moc.html#multiple-inheritance-requires-qobject-to-be-first
If you are using multiple inheritance,
mocassumes that the first inherited class is a subclass ofQObject. Also, be sure that only the first inherited class is aQObject.I think that you not have at all to inherit from Page class on your ImageManagerPage class at all,
For me:
You need just to add a setter on your Page class
class Page { public: . . . void setPageTitle(&argPageTitle);//Here this my title . . . private: QString PageTitle; };And just include Page class on your ImageManagerPage :
class ImageManagerPage : public QWidget { Q_OBJECT public: explicit ImageManagerPage(QWidget *parent = 0); ~ImageManagerPage(); private: Page mPage; signals: public slots: };And then you have just to do:
QString title= "Image Manager";.
mPage.setPageTitle(&title);and that's it
Hope this can help !
-
Hi,
To add to @mostefa, why not make your page a QWidget based class directly and then re-use it to build your other widgets ?
@SGaist said in Qt inheritance problem:
Hi,
To add to @mostefa, why not make your page a QWidget based class directly and then re-use it to build your other widgets ?
I am totally OK with you @SGaist , And i think that your proposition is the best thing to do.
-
Hi,
To add to @mostefa, why not make your page a QWidget based class directly and then re-use it to build your other widgets ?
