跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. General and Desktop
  4. Access and inheritance
Forum Updated to NodeBB v4.3 + New Features

Access and inheritance

已排程 已置頂 已鎖定 已移動 Solved General and Desktop
49 貼文 5 Posters 11.8k 瀏覽 4 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • mrjjM mrjj

    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.

    tomyT 離線
    tomyT 離線
    tomy
    寫於 最後由 編輯
    #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?

    mrjjM 1 條回覆 最後回覆
    0
    • tomyT tomy

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

      mrjjM 離線
      mrjjM 離線
      mrjj
      Lifetime Qt Champion
      寫於 最後由 mrjj 編輯
      #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?

      tomyT 1 條回覆 最後回覆
      3
      • mrjjM mrjj

        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?

        tomyT 離線
        tomyT 離線
        tomy
        寫於 最後由 編輯
        #13

        @mrjj hi,

        The spreadsheet?

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

        mrjjM 1 條回覆 最後回覆
        0
        • tomyT tomy

          @mrjj hi,

          The spreadsheet?

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

          mrjjM 離線
          mrjjM 離線
          mrjj
          Lifetime Qt Champion
          寫於 最後由 編輯
          #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.

          tomyT 1 條回覆 最後回覆
          1
          • mrjjM mrjj

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

            tomyT 離線
            tomyT 離線
            tomy
            寫於 最後由 編輯
            #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). :)

            mrjjM 1 條回覆 最後回覆
            0
            • tomyT tomy

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

              mrjjM 離線
              mrjjM 離線
              mrjj
              Lifetime Qt Champion
              寫於 最後由 編輯
              #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.

              tomyT 1 條回覆 最後回覆
              1
              • mrjjM mrjj

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

                tomyT 離線
                tomyT 離線
                tomy
                寫於 最後由 編輯
                #17

                @mrjj

                So what does it have now ?

                this:

                0_1549822272949_Capture.PNG

                mrjjM 1 條回覆 最後回覆
                0
                • tomyT tomy

                  @mrjj

                  So what does it have now ?

                  this:

                  0_1549822272949_Capture.PNG

                  mrjjM 離線
                  mrjjM 離線
                  mrjj
                  Lifetime Qt Champion
                  寫於 最後由 編輯
                  #18

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

                  tomyT 1 條回覆 最後回覆
                  0
                  • mrjjM mrjj

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

                    tomyT 離線
                    tomyT 離線
                    tomy
                    寫於 最後由 編輯
                    #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?

                    mrjjM 1 條回覆 最後回覆
                    0
                    • tomyT tomy

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

                      mrjjM 離線
                      mrjjM 離線
                      mrjj
                      Lifetime Qt Champion
                      寫於 最後由 編輯
                      #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.

                      tomyT 1 條回覆 最後回覆
                      2
                      • mrjjM mrjj

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

                        tomyT 離線
                        tomyT 離線
                        tomy
                        寫於 最後由 編輯
                        #21

                        @mrjj

                        Thank you very much. I was too much stuck in a QML program's problem, sorry for the delay.

                        If you run the program, even by the latest version of Qt Creator and on a sophisticated and fancy operating system like Windows 10, you see the program still looks old, as though we are in 2000 using it.
                        Is it because of the code? If so, what parts can be updated, please?

                        mrjjM jsulmJ 2 條回覆 最後回覆
                        0
                        • tomyT tomy

                          @mrjj

                          Thank you very much. I was too much stuck in a QML program's problem, sorry for the delay.

                          If you run the program, even by the latest version of Qt Creator and on a sophisticated and fancy operating system like Windows 10, you see the program still looks old, as though we are in 2000 using it.
                          Is it because of the code? If so, what parts can be updated, please?

                          mrjjM 離線
                          mrjjM 離線
                          mrjj
                          Lifetime Qt Champion
                          寫於 最後由 編輯
                          #22

                          @tomy
                          Hi
                          Nope its just how the QWidgets look.
                          If you change the icons to flat style,
                          it will look more "modern"

                          You could use something like
                          https://github.com/laserpants/qt-material-widgets
                          if you want to go all in for "modern" look.

                          tomyT 1 條回覆 最後回覆
                          2
                          • tomyT tomy

                            @mrjj

                            Thank you very much. I was too much stuck in a QML program's problem, sorry for the delay.

                            If you run the program, even by the latest version of Qt Creator and on a sophisticated and fancy operating system like Windows 10, you see the program still looks old, as though we are in 2000 using it.
                            Is it because of the code? If so, what parts can be updated, please?

                            jsulmJ 線上
                            jsulmJ 線上
                            jsulm
                            Lifetime Qt Champion
                            寫於 最後由 編輯
                            #23

                            @tomy Do you use Qt4 or Qt5?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            tomyT 1 條回覆 最後回覆
                            0
                            • jsulmJ jsulm

                              @tomy Do you use Qt4 or Qt5?

                              tomyT 離線
                              tomyT 離線
                              tomy
                              寫於 最後由 編輯
                              #24

                              @jsulm
                              It's 5.12. The latest version I think.

                              1 條回覆 最後回覆
                              0
                              • mrjjM mrjj

                                @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

                                kshegunovK 離線
                                kshegunovK 離線
                                kshegunov
                                Moderators
                                寫於 最後由 kshegunov 編輯
                                #25

                                @mrjj said in Access and inheritance:

                                The truth is, if a base class do not do anything in its constructor, nothing bad will happen not calling it.

                                Just for completeness:
                                If the base class has a default constructor, and it's not called explicitly in the derived class' constructor the compiler is going to generate code to call it implicitly. Omitting it in the initializer list means nothing here, as QMainWindow() is going to be called either way. If the base class does not have a default constructor, and there's no call to the parent's constructor then this is going to generate a compile error, as the compiler has no idea what to do.

                                Read and abide by the Qt Code of Conduct

                                tomyT 1 條回覆 最後回覆
                                4
                                • kshegunovK kshegunov

                                  @mrjj said in Access and inheritance:

                                  The truth is, if a base class do not do anything in its constructor, nothing bad will happen not calling it.

                                  Just for completeness:
                                  If the base class has a default constructor, and it's not called explicitly in the derived class' constructor the compiler is going to generate code to call it implicitly. Omitting it in the initializer list means nothing here, as QMainWindow() is going to be called either way. If the base class does not have a default constructor, and there's no call to the parent's constructor then this is going to generate a compile error, as the compiler has no idea what to do.

                                  tomyT 離線
                                  tomyT 離線
                                  tomy
                                  寫於 最後由 編輯
                                  #26

                                  @kshegunov
                                  Hi,

                                  If the base class has a default constructor, and it's not called explicitly in the derived class' constructor the compiler is going to generate code to call it implicitly. Omitting it in the initializer list means nothing here, as QMainWindow() is going to be called either way.

                                  Is it true for the Qt Creator compiler too? (I think so)

                                  If the base class does not have a default constructor, and there's no call to the parent's constructor then this is going to generate a compile error, as the compiler has no idea what to do.

                                  Can we conclude this way that, when the base class doesn't have a constructor, in either way, whether the subclass calls the parent's constructor (!) or it doesn't, we will get an error?

                                  kshegunovK 1 條回覆 最後回覆
                                  0
                                  • tomyT tomy

                                    @kshegunov
                                    Hi,

                                    If the base class has a default constructor, and it's not called explicitly in the derived class' constructor the compiler is going to generate code to call it implicitly. Omitting it in the initializer list means nothing here, as QMainWindow() is going to be called either way.

                                    Is it true for the Qt Creator compiler too? (I think so)

                                    If the base class does not have a default constructor, and there's no call to the parent's constructor then this is going to generate a compile error, as the compiler has no idea what to do.

                                    Can we conclude this way that, when the base class doesn't have a constructor, in either way, whether the subclass calls the parent's constructor (!) or it doesn't, we will get an error?

                                    kshegunovK 離線
                                    kshegunovK 離線
                                    kshegunov
                                    Moderators
                                    寫於 最後由 編輯
                                    #27

                                    @tomy said in Access and inheritance:

                                    Is it true for the Qt Creator compiler too? (I think so)

                                    Qt Creator is not a compiler, it uses some compiler (whatever you've configured), but yes, it's true for all compiler, as this is standard (and required) behaviour for C++.

                                    Can we conclude this way that, when the base class doesn't have a constructor, in either way, whether the subclass calls the parent's constructor (!) or it doesn't, we will get an error?

                                    No. Not defining a class constructor means the compiler generates a default one for you (unless specifically told otherwise). If you haven't defined a constructor in the base class, then the derived class is going to implicitly call the automatically generated one.

                                    Read and abide by the Qt Code of Conduct

                                    tomyT 1 條回覆 最後回覆
                                    3
                                    • kshegunovK kshegunov

                                      @tomy said in Access and inheritance:

                                      Is it true for the Qt Creator compiler too? (I think so)

                                      Qt Creator is not a compiler, it uses some compiler (whatever you've configured), but yes, it's true for all compiler, as this is standard (and required) behaviour for C++.

                                      Can we conclude this way that, when the base class doesn't have a constructor, in either way, whether the subclass calls the parent's constructor (!) or it doesn't, we will get an error?

                                      No. Not defining a class constructor means the compiler generates a default one for you (unless specifically told otherwise). If you haven't defined a constructor in the base class, then the derived class is going to implicitly call the automatically generated one.

                                      tomyT 離線
                                      tomyT 離線
                                      tomy
                                      寫於 最後由 編輯
                                      #28

                                      @kshegunov

                                      So we will never have to call the base class's constructor from the subclass, because if there is an explicit constructor for the base class, the compiler calls (implicitly) for us (with the absence of an explicit call from us), and if there is no constructor for the base class, the compiler creates a default one and implicitly calls that in the subclass. Hence, we had better never involve ourselves with calling a base class's constructor directly (from the subclass)! :) :)

                                      mrjjM kshegunovK 2 條回覆 最後回覆
                                      0
                                      • tomyT tomy

                                        @kshegunov

                                        So we will never have to call the base class's constructor from the subclass, because if there is an explicit constructor for the base class, the compiler calls (implicitly) for us (with the absence of an explicit call from us), and if there is no constructor for the base class, the compiler creates a default one and implicitly calls that in the subclass. Hence, we had better never involve ourselves with calling a base class's constructor directly (from the subclass)! :) :)

                                        mrjjM 離線
                                        mrjjM 離線
                                        mrjj
                                        Lifetime Qt Champion
                                        寫於 最後由 mrjj 編輯
                                        #29

                                        @tomy
                                        Hi
                                        The automatic version is only for a default constructor.
                                        As soon as base class constructor takes parameters, it must be called from subclass as compiler cannot
                                        know how to obtain the parameters to give to base class.
                                        However the compiler will tell you that.

                                        1 條回覆 最後回覆
                                        3
                                        • tomyT tomy

                                          @kshegunov

                                          So we will never have to call the base class's constructor from the subclass, because if there is an explicit constructor for the base class, the compiler calls (implicitly) for us (with the absence of an explicit call from us), and if there is no constructor for the base class, the compiler creates a default one and implicitly calls that in the subclass. Hence, we had better never involve ourselves with calling a base class's constructor directly (from the subclass)! :) :)

                                          kshegunovK 離線
                                          kshegunovK 離線
                                          kshegunov
                                          Moderators
                                          寫於 最後由 編輯
                                          #30

                                          What @mrjj said, plus the moment you define a constructor, the compiler stops generating for you. Thus if you define a constructor with parameters, the compiler will not generate a default constructor for you. Beautiful system, right? :)

                                          Read and abide by the Qt Code of Conduct

                                          tomyT 1 條回覆 最後回覆
                                          3

                                          • 登入

                                          • Login or register to search.
                                          • 第一個貼文
                                            最後的貼文
                                          0
                                          • 版面
                                          • 最新
                                          • 標籤
                                          • 熱門
                                          • 使用者
                                          • 群組
                                          • 搜尋
                                          • Get Qt Extensions
                                          • Unsolved