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. Custom context menu item
Forum Updated to NodeBB v4.3 + New Features

Custom context menu item

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 2.3k Views 2 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.
  • G Offline
    G Offline
    Grand_Titan
    wrote on last edited by
    #1

    I want to add a context menu item that will execute a function. The context menu appears after right clicking a QPlainTextEdit.3a256f3c-870a-45f7-b5fa-aa48471a783e-image.png

    Christian EhrlicherC 1 Reply Last reply
    0
    • G Grand_Titan

      @JonB I'm new to c++ and I'm still not quite familiar with subclassing and overriding methods. Also I'm not sure how to do it because I already created my QPlainTextEdit in the editor and made its style and a lot of my program depends on its existence. Im not sure how to account for all of that while doing something like an override. Is it possible to do something like this with an event filter? Because I already have an event filter installed on this object.

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

      @Grand_Titan
      There's an alternative that doesn't require subclassing.

      First set the policy:
      yourPlainText->setContextMenuPolicy(Qt::CustomContextMenu);
      then connect your main window to receive the customContextMenuRequested signal.

      In a slot in your main window you can do like:

      showContextMenu(const QPoint &)
      {
          QMenu* menu=yourPlainText->createStandardContextMenu();
          menu->addSeparator();
      // create your action
          QAction* action=menu->addAction(tr("your action"));
      // then connect your action to a slot in your main window
          connect(action, ....);
         
       menu->exec(QCursor::pos());
      
       menu->deleteLater();
      }
      
      
      SGaistS 1 Reply Last reply
      2
      • G Grand_Titan

        I want to add a context menu item that will execute a function. The context menu appears after right clicking a QPlainTextEdit.3a256f3c-870a-45f7-b5fa-aa48471a783e-image.png

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        You have to override customContextMenuEvent().

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        G 1 Reply Last reply
        4
        • Christian EhrlicherC Christian Ehrlicher

          You have to override customContextMenuEvent().

          G Offline
          G Offline
          Grand_Titan
          wrote on last edited by
          #3

          @Christian-Ehrlicher How can I do that?

          JonBJ 1 Reply Last reply
          0
          • G Grand_Titan

            @Christian-Ehrlicher How can I do that?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Grand_Titan
            How do you mean? Override the method, put whatever code you want it to execute in the override. Simple example code is in the linked page, what do you want to achieve beyond what it shows you to do?

            G 2 Replies Last reply
            0
            • JonBJ JonB

              @Grand_Titan
              How do you mean? Override the method, put whatever code you want it to execute in the override. Simple example code is in the linked page, what do you want to achieve beyond what it shows you to do?

              G Offline
              G Offline
              Grand_Titan
              wrote on last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • JonBJ JonB

                @Grand_Titan
                How do you mean? Override the method, put whatever code you want it to execute in the override. Simple example code is in the linked page, what do you want to achieve beyond what it shows you to do?

                G Offline
                G Offline
                Grand_Titan
                wrote on last edited by
                #6

                @JonB I'm new to c++ and I'm still not quite familiar with subclassing and overriding methods. Also I'm not sure how to do it because I already created my QPlainTextEdit in the editor and made its style and a lot of my program depends on its existence. Im not sure how to account for all of that while doing something like an override. Is it possible to do something like this with an event filter? Because I already have an event filter installed on this object.

                JonBJ M 2 Replies Last reply
                0
                • G Grand_Titan

                  @JonB I'm new to c++ and I'm still not quite familiar with subclassing and overriding methods. Also I'm not sure how to do it because I already created my QPlainTextEdit in the editor and made its style and a lot of my program depends on its existence. Im not sure how to account for all of that while doing something like an override. Is it possible to do something like this with an event filter? Because I already have an event filter installed on this object.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @Grand_Titan
                  In the longer run you absolutely must get familiar with C++ subclassing/overriding! And there will be times in Qt where you cannot get away without this.

                  It is a problem if you use the Qt base classes in Designer because as you note, you then must subclass in order to do certain things. Read about how Qt Creator allows you to promote widgets you have subclassed so you can still set them from Designer.

                  For this occasion, QEvent::type has

                  QEvent::ContextMenu 82 Context popup menu (QContextMenuEvent).

                  so try using that in installed filter on existing QPlainTextEdit to achieve without subclassing?

                  1 Reply Last reply
                  2
                  • G Grand_Titan

                    @JonB I'm new to c++ and I'm still not quite familiar with subclassing and overriding methods. Also I'm not sure how to do it because I already created my QPlainTextEdit in the editor and made its style and a lot of my program depends on its existence. Im not sure how to account for all of that while doing something like an override. Is it possible to do something like this with an event filter? Because I already have an event filter installed on this object.

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

                    @Grand_Titan
                    There's an alternative that doesn't require subclassing.

                    First set the policy:
                    yourPlainText->setContextMenuPolicy(Qt::CustomContextMenu);
                    then connect your main window to receive the customContextMenuRequested signal.

                    In a slot in your main window you can do like:

                    showContextMenu(const QPoint &)
                    {
                        QMenu* menu=yourPlainText->createStandardContextMenu();
                        menu->addSeparator();
                    // create your action
                        QAction* action=menu->addAction(tr("your action"));
                    // then connect your action to a slot in your main window
                        connect(action, ....);
                       
                     menu->exec(QCursor::pos());
                    
                     menu->deleteLater();
                    }
                    
                    
                    SGaistS 1 Reply Last reply
                    2
                    • M mpergand

                      @Grand_Titan
                      There's an alternative that doesn't require subclassing.

                      First set the policy:
                      yourPlainText->setContextMenuPolicy(Qt::CustomContextMenu);
                      then connect your main window to receive the customContextMenuRequested signal.

                      In a slot in your main window you can do like:

                      showContextMenu(const QPoint &)
                      {
                          QMenu* menu=yourPlainText->createStandardContextMenu();
                          menu->addSeparator();
                      // create your action
                          QAction* action=menu->addAction(tr("your action"));
                      // then connect your action to a slot in your main window
                          connect(action, ....);
                         
                       menu->exec(QCursor::pos());
                      
                       menu->deleteLater();
                      }
                      
                      
                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by SGaist
                      #9

                      @mpergand i would change juste one thing: make the QMenu a stack object since you are using exec. Hence no need for explicit deletion later

                      Edit: misread the first line, that's the correct way to include the default menu.

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

                      M 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        @mpergand i would change juste one thing: make the QMenu a stack object since you are using exec. Hence no need for explicit deletion later

                        Edit: misread the first line, that's the correct way to include the default menu.

                        M Offline
                        M Offline
                        mpergand
                        wrote on last edited by
                        #10

                        @SGaist
                        But in the case of a QPlainText for ex, with createStandardContextMenu() you get the standard items like copy, paste etc, for free ;)

                        SGaistS 1 Reply Last reply
                        0
                        • M mpergand

                          @SGaist
                          But in the case of a QPlainText for ex, with createStandardContextMenu() you get the standard items like copy, paste etc, for free ;)

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @mpergand my bad ! I misread the first line. You are completely correct :-)

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

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            Grand_Titan
                            wrote on last edited by
                            #12

                            Thank you all. I think ill be able to achieve this now. Also thanks for the suggestion I will dig deeper into that stuff.

                            1 Reply Last reply
                            0
                            • G Grand_Titan has marked this topic as solved on

                            • Login

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