Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt inheritance problem
Forum Updated to NodeBB v4.3 + New Features

Qt inheritance problem

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phong
    wrote on last edited by Phong
    #1

    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,

    1. I'm not sure is this the right way to inherit a class??
    2. 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~!!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by mostefa
      #2

      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!

      P 1 Reply Last reply
      4
      • M mostefa

        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!

        P Offline
        P Offline
        Phong
        wrote on last edited by
        #3

        @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??

        M 1 Reply Last reply
        0
        • P Phong

          @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??

          M Offline
          M Offline
          mostefa
          wrote on last edited by mostefa
          #4

          @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 QObject at all,this will avoid conflict, cause If

          you will inherit from QObject on your page class
          than inherit from QWidget, page on your ImageManagerPage

          This 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 QObject

          So 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, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

          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 !

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M P 2 Replies Last reply
            2
            • SGaistS SGaist

              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 ?

              M Offline
              M Offline
              mostefa
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • SGaistS SGaist

                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 ?

                P Offline
                P Offline
                Phong
                wrote on last edited by
                #7

                @SGaist
                That might be the best way , thanks a lot!!
                BTW, how to mark the topic as SOLVED?

                M 1 Reply Last reply
                0
                • P Phong

                  @SGaist
                  That might be the best way , thanks a lot!!
                  BTW, how to mark the topic as SOLVED?

                  M Offline
                  M Offline
                  mostefa
                  wrote on last edited by
                  #8

                  @Phong said in Qt inheritance problem:

                  @SGaist
                  That might be the best way , thanks a lot!!
                  BTW, how to mark the topic as SOLVED?

                  On the bottom of your post,

                  Topic tools,==> mark as solved

                  alt text

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved