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. Access and inheritance
Forum Updated to NodeBB v4.3 + New Features

Access and inheritance

Scheduled Pinned Locked Moved Solved General and Desktop
49 Posts 5 Posters 11.2k Views 4 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.
  • T Offline
    T Offline
    tomy
    wrote on 9 Feb 2019, 09:28 last edited by
    #1

    Hi,

    Have you studied the spreadsheet program of the official Qt book C++-GUI-Programming-with-Qt-4-2ndEdition?
    There in the createActions() method we have below:

    showGridAction->setChecked(spreadsheet->showGrid());

    where the spreadsheet's header/cpp doesn't have such a method showGrid(), but the spreadsheet inherits from QTableWidget inside which the showGrid() method exists.
    Yes, when that function is called, in reality, it goes and calls QTableWidget's method, I understand that.
    But I think there must have been a method with this name in spreadsheet and inside its definition it should have called its parent's (QTableWidget)showGrid() method.

    Am I not right?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 Feb 2019, 11:17 last edited by
      #2

      Hi
      You inherit the function into the spreadsheet class, so its callable via spreadsheet without it defining it also.
      It simply calls the base class function.
      Notice however, you inherited public, private or protected which
      decides what function you can call.
      So if you inherited public, you cannot call private functions via the sub class.

      T 1 Reply Last reply 10 Feb 2019, 07:28
      3
      • M mrjj
        9 Feb 2019, 11:17

        Hi
        You inherit the function into the spreadsheet class, so its callable via spreadsheet without it defining it also.
        It simply calls the base class function.
        Notice however, you inherited public, private or protected which
        decides what function you can call.
        So if you inherited public, you cannot call private functions via the sub class.

        T Offline
        T Offline
        tomy
        wrote on 10 Feb 2019, 07:28 last edited by
        #3

        @mrjj
        Thank you. It's actually C++, I don't know why I have forgotten it! :(

        Yes, it's public inheritance.
        So, for example, when spreadsheet inherits from QTableWidget publicly, it's just like spreadsheet having a copy of all public functions of QTableWidget inside its public area. Right?

        M 1 Reply Last reply 10 Feb 2019, 09:13
        0
        • T tomy
          10 Feb 2019, 07:28

          @mrjj
          Thank you. It's actually C++, I don't know why I have forgotten it! :(

          Yes, it's public inheritance.
          So, for example, when spreadsheet inherits from QTableWidget publicly, it's just like spreadsheet having a copy of all public functions of QTableWidget inside its public area. Right?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 10 Feb 2019, 09:13 last edited by mrjj 2 Oct 2019, 09:33
          #4

          @tomy
          Hi
          One could say that, but its actually more than a copy of the functions.
          The base class is part of the sub class.
          That is why we need to initialize the base from the subclass when subclass get constructed.

          If you look in Qt samples. You will notice we always call the base class constructor

          MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { << this part calls QMainWindow constructor with the parent parameter.
          ...
          }

          so from subclass MainWindow constructor we also call base class QMainWindow constructor so it can be setup also.

          T 1 Reply Last reply 10 Feb 2019, 10:07
          2
          • M mrjj
            10 Feb 2019, 09:13

            @tomy
            Hi
            One could say that, but its actually more than a copy of the functions.
            The base class is part of the sub class.
            That is why we need to initialize the base from the subclass when subclass get constructed.

            If you look in Qt samples. You will notice we always call the base class constructor

            MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { << this part calls QMainWindow constructor with the parent parameter.
            ...
            }

            so from subclass MainWindow constructor we also call base class QMainWindow constructor so it can be setup also.

            T Offline
            T Offline
            tomy
            wrote on 10 Feb 2019, 10:07 last edited by
            #5

            @mrjj
            Hi,
            you're right, but where in the constructor of MainWindow is the constructor of QMainWindow called?

            0_1549793249628_1.PNG
            0_1549793079015_Capture.PNG

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 10 Feb 2019, 10:18 last edited by
              #6

              Hi
              In that sample it seems forgotten for mainwindow.
              But they do for spreadsheet
              Spreadsheet::Spreadsheet(QWidget *parent)
              : QTableWidget(parent) <<<<<
              {

              T 1 Reply Last reply 10 Feb 2019, 10:26
              0
              • M mrjj
                10 Feb 2019, 10:18

                Hi
                In that sample it seems forgotten for mainwindow.
                But they do for spreadsheet
                Spreadsheet::Spreadsheet(QWidget *parent)
                : QTableWidget(parent) <<<<<
                {

                T Offline
                T Offline
                tomy
                wrote on 10 Feb 2019, 10:26 last edited by
                #7

                @mrjj
                Hi,
                So could we have QMainWindow as a subclass of MainWindow without calling its constructor in the constructor of MainWindow?

                By the way, the book says MainWindow as a subclass of QMainWinodw!
                It seems that both are subclasses of each other! :)

                M 1 Reply Last reply 10 Feb 2019, 10:40
                0
                • T tomy
                  10 Feb 2019, 10:26

                  @mrjj
                  Hi,
                  So could we have QMainWindow as a subclass of MainWindow without calling its constructor in the constructor of MainWindow?

                  By the way, the book says MainWindow as a subclass of QMainWinodw!
                  It seems that both are subclasses of each other! :)

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 10 Feb 2019, 10:40 last edited by
                  #8

                  @tomy
                  hi
                  well its MainWindow as a subclass of QMainWindow
                  The names are so close its sometimes confusing.
                  The truth is, if a base class do not do anything in its constructor, nothing bad will happen not calling it.
                  Which seems why they didnt bother here for MainWindow

                  T kshegunovK 2 Replies Last reply 10 Feb 2019, 13:37
                  2
                  • M mrjj
                    10 Feb 2019, 10:40

                    @tomy
                    hi
                    well its MainWindow as a subclass of QMainWindow
                    The names are so close its sometimes confusing.
                    The truth is, if a base class do not do anything in its constructor, nothing bad will happen not calling it.
                    Which seems why they didnt bother here for MainWindow

                    T Offline
                    T Offline
                    tomy
                    wrote on 10 Feb 2019, 13:37 last edited by
                    #9

                    @mrjj
                    Hi, thanks, I got it.

                    One other question. In page 70 of that book, about context menu, the following is written:
                    "A more sophisticated way of providing context menus is to reimplement the QWidget::contextMenuEvent() function,
                    create a QMenu widget, populate it with the desired actions, and call exec() on it.
                    "

                    If I want that sophisticated context menu, I need to reimplement the QWidget::contextMenuEvent() function. I found that here, but how to know its code to be able to change that and use it for the application please?

                    M 1 Reply Last reply 10 Feb 2019, 15:40
                    0
                    • T tomy
                      10 Feb 2019, 13:37

                      @mrjj
                      Hi, thanks, I got it.

                      One other question. In page 70 of that book, about context menu, the following is written:
                      "A more sophisticated way of providing context menus is to reimplement the QWidget::contextMenuEvent() function,
                      create a QMenu widget, populate it with the desired actions, and call exec() on it.
                      "

                      If I want that sophisticated context menu, I need to reimplement the QWidget::contextMenuEvent() function. I found that here, but how to know its code to be able to change that and use it for the application please?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 10 Feb 2019, 15:40 last edited by mrjj 2 Oct 2019, 17:05
                      #10

                      Hi
                      You would create a new subclass.
                      However, it seldom useful for just a context menu and a plain QWidget.
                      So where do you want to use the context menu ?

                      anyway for pure learning.

                      
                      class MyWidget : public QWidget
                      {
                          Q_OBJECT
                      public:
                          explicit MyWidget(QWidget*parent = nullptr) : QWidget(parent)
                          {
                      
                          }
                      protected:
                      // here we then override the defualt one we get from base class to have our own.
                          virtual void contextMenuEvent(QContextMenuEvent *event) override
                          {
                              .....make menu....
                          }
                      };
                      
                      

                      To use it you would either
                      create from code
                      MyWidget *wid=new MyWidget(this)
                      and insert into layout / form.

                      or use Creators promotion feature to allow to use in Designer.
                      https://doc.qt.io/qt-5/designer-using-custom-widgets.html

                      Note that we here inherit from QWidget
                      However, it could have been from any widget. Like QPushButton etc.

                      T 1 Reply Last reply 10 Feb 2019, 16:40
                      2
                      • M mrjj
                        10 Feb 2019, 15:40

                        Hi
                        You would create a new subclass.
                        However, it seldom useful for just a context menu and a plain QWidget.
                        So where do you want to use the context menu ?

                        anyway for pure learning.

                        
                        class MyWidget : public QWidget
                        {
                            Q_OBJECT
                        public:
                            explicit MyWidget(QWidget*parent = nullptr) : QWidget(parent)
                            {
                        
                            }
                        protected:
                        // here we then override the defualt one we get from base class to have our own.
                            virtual void contextMenuEvent(QContextMenuEvent *event) override
                            {
                                .....make menu....
                            }
                        };
                        
                        

                        To use it you would either
                        create from code
                        MyWidget *wid=new MyWidget(this)
                        and insert into layout / form.

                        or use Creators promotion feature to allow to use in Designer.
                        https://doc.qt.io/qt-5/designer-using-custom-widgets.html

                        Note that we here inherit from QWidget
                        However, it could have been from any widget. Like QPushButton etc.

                        T Offline
                        T Offline
                        tomy
                        wrote on 10 Feb 2019, 16:40 last edited by
                        #11

                        @mrjj
                        Hi, thanks.

                        I forgot to say that I declared it inside the protected modifier area of MainWindow this way:

                        ...
                        protected:
                            void closeEvent(QCloseEvent*); // Previousely added
                            void contextMenuEvent(QContextMenuEvent*); // I, too, added this one
                        ...
                        

                        and it's pure code.

                        I want to use it into the spreadsheet program we talked about, to manipulate the code to have a better context menu for that program.
                        One question as well, what will we achieve by re-implementing that virtual protected method please?

                        M 1 Reply Last reply 10 Feb 2019, 16:57
                        0
                        • T tomy
                          10 Feb 2019, 16:40

                          @mrjj
                          Hi, thanks.

                          I forgot to say that I declared it inside the protected modifier area of MainWindow this way:

                          ...
                          protected:
                              void closeEvent(QCloseEvent*); // Previousely added
                              void contextMenuEvent(QContextMenuEvent*); // I, too, added this one
                          ...
                          

                          and it's pure code.

                          I want to use it into the spreadsheet program we talked about, to manipulate the code to have a better context menu for that program.
                          One question as well, what will we achieve by re-implementing that virtual protected method please?

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 10 Feb 2019, 16:57 last edited by mrjj 2 Oct 2019, 17:00
                          #12

                          Hi
                          If you add contextMenuEvent to MainWindow, then you need
                          to right click the actual MainWindow, to trigger it.
                          It wont be triggered by right clicking on the spreadSheet widget.
                          So not sure having them in MainWindow is what you want ?

                          • One question as well, what will we achieve by re-implementing that virtual protected method please?

                          Well it allows us to override a default contextMenuEvent so for any widget that can actually show a menu already( like QlineEdit)
                          it allows to to alter what should be shown ( a new menu )
                          Or it allows us to add a menu to a QWidget that dont already have such menu.

                          BUT
                          Often the signal is used instead (of the Event ) as that requires no subclass.
                          https://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested
                          That you can just connect to and do get the same effect without subclassing.

                          However, WHAT widget do you want to add contextMenu to ?
                          The spreadsheet?

                          T 1 Reply Last reply 10 Feb 2019, 17:33
                          3
                          • M mrjj
                            10 Feb 2019, 16:57

                            Hi
                            If you add contextMenuEvent to MainWindow, then you need
                            to right click the actual MainWindow, to trigger it.
                            It wont be triggered by right clicking on the spreadSheet widget.
                            So not sure having them in MainWindow is what you want ?

                            • One question as well, what will we achieve by re-implementing that virtual protected method please?

                            Well it allows us to override a default contextMenuEvent so for any widget that can actually show a menu already( like QlineEdit)
                            it allows to to alter what should be shown ( a new menu )
                            Or it allows us to add a menu to a QWidget that dont already have such menu.

                            BUT
                            Often the signal is used instead (of the Event ) as that requires no subclass.
                            https://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested
                            That you can just connect to and do get the same effect without subclassing.

                            However, WHAT widget do you want to add contextMenu to ?
                            The spreadsheet?

                            T Offline
                            T Offline
                            tomy
                            wrote on 10 Feb 2019, 17:33 last edited by
                            #13

                            @mrjj hi,

                            The spreadsheet?

                            Yes. The area occupied by the cells. It actually will be used for cells.

                            M 1 Reply Last reply 10 Feb 2019, 17:39
                            0
                            • T tomy
                              10 Feb 2019, 17:33

                              @mrjj hi,

                              The spreadsheet?

                              Yes. The area occupied by the cells. It actually will be used for cells.

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 10 Feb 2019, 17:39 last edited by
                              #14

                              @tomy

                              Hi I would use the SIGNAL then.
                              I assume it has not right click menu already?
                              To use it, you enable it with
                              ui->spreadsheet->setContextMenuPolicy(Qt::CustomContextMenu);
                              (ui->spreadsheet might not be actual name ;)

                              then connect signal to a slot where you build and exec() the menu.

                              T 1 Reply Last reply 10 Feb 2019, 17:44
                              1
                              • M mrjj
                                10 Feb 2019, 17:39

                                @tomy

                                Hi I would use the SIGNAL then.
                                I assume it has not right click menu already?
                                To use it, you enable it with
                                ui->spreadsheet->setContextMenuPolicy(Qt::CustomContextMenu);
                                (ui->spreadsheet might not be actual name ;)

                                then connect signal to a slot where you build and exec() the menu.

                                T Offline
                                T Offline
                                tomy
                                wrote on 10 Feb 2019, 17:44 last edited by
                                #15

                                @mrjj

                                I assume it has not right click menu already?

                                It has. The author said: "A more sophisticated way of providing context menus is to reimplement the QWidget::contextMenuEvent() function,
                                create a QMenu widget, populate it with the desired actions, and call exec() on it.", so I was motivated to re-implement that method to probably have a nicer or more advanced context menu for the cells!
                                By the way, I like to use pure code only (no design mode). :)

                                M 1 Reply Last reply 10 Feb 2019, 17:47
                                0
                                • T tomy
                                  10 Feb 2019, 17:44

                                  @mrjj

                                  I assume it has not right click menu already?

                                  It has. The author said: "A more sophisticated way of providing context menus is to reimplement the QWidget::contextMenuEvent() function,
                                  create a QMenu widget, populate it with the desired actions, and call exec() on it.", so I was motivated to re-implement that method to probably have a nicer or more advanced context menu for the cells!
                                  By the way, I like to use pure code only (no design mode). :)

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 10 Feb 2019, 17:47 last edited by
                                  #16

                                  @tomy
                                  Well im not sure what he meant by that since its just a QMenu in any case.

                                  So what does it have now ?

                                  Im not sure why using event would be more sophisticated. Its just other way
                                  of having a context menu.

                                  T 1 Reply Last reply 10 Feb 2019, 18:11
                                  1
                                  • M mrjj
                                    10 Feb 2019, 17:47

                                    @tomy
                                    Well im not sure what he meant by that since its just a QMenu in any case.

                                    So what does it have now ?

                                    Im not sure why using event would be more sophisticated. Its just other way
                                    of having a context menu.

                                    T Offline
                                    T Offline
                                    tomy
                                    wrote on 10 Feb 2019, 18:11 last edited by
                                    #17

                                    @mrjj

                                    So what does it have now ?

                                    this:

                                    0_1549822272949_Capture.PNG

                                    M 1 Reply Last reply 10 Feb 2019, 18:18
                                    0
                                    • T tomy
                                      10 Feb 2019, 18:11

                                      @mrjj

                                      So what does it have now ?

                                      this:

                                      0_1549822272949_Capture.PNG

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 10 Feb 2019, 18:18 last edited by
                                      #18

                                      @tomy
                                      Hi
                                      Is that build from code or a default one ?
                                      I mean is there code to build it and show it ?

                                      T 1 Reply Last reply 10 Feb 2019, 18:24
                                      0
                                      • M mrjj
                                        10 Feb 2019, 18:18

                                        @tomy
                                        Hi
                                        Is that build from code or a default one ?
                                        I mean is there code to build it and show it ?

                                        T Offline
                                        T Offline
                                        tomy
                                        wrote on 10 Feb 2019, 18:24 last edited by
                                        #19

                                        @mrjj Hi,
                                        Yes. The book also shows the code for that.
                                        I know I can add more options to that, for instance, to have five buttons in the context menu rather than that three ones. But by the sentence the author said, I though there should be another way to have a more advanced context menu by re-implementing the virtual protected method when mentioned earlier.

                                        By the way, don't you have that book?

                                        M 1 Reply Last reply 10 Feb 2019, 18:29
                                        0
                                        • T tomy
                                          10 Feb 2019, 18:24

                                          @mrjj Hi,
                                          Yes. The book also shows the code for that.
                                          I know I can add more options to that, for instance, to have five buttons in the context menu rather than that three ones. But by the sentence the author said, I though there should be another way to have a more advanced context menu by re-implementing the virtual protected method when mentioned earlier.

                                          By the way, don't you have that book?

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 10 Feb 2019, 18:29 last edited by
                                          #20

                                          @tomy
                                          Hi
                                          Well the menu already have both shortcut and icon so not sure what else could be added.
                                          There is no "more advanced" menu possible by overwriting contextMenuEvent as
                                          you can just 100% the same just altering
                                          void MainWindow::createActions()
                                          to show what you want.

                                          yes, i have it as PDF. did read most of it way back.

                                          T 1 Reply Last reply 12 Feb 2019, 08:32
                                          2

                                          1/49

                                          9 Feb 2019, 09:28

                                          • Login

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