Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to create a context menu

How to create a context menu

Scheduled Pinned Locked Moved Solved Qt for Python
10 Posts 4 Posters 2.0k Views
  • 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.
  • L Offline
    L Offline
    LT-K101
    wrote on 10 Oct 2022, 12:18 last edited by
    #1

    I want a context menu to work with a specific TableWidget but when I click anywhere in the application the context menu pops up. Below is the code I tried, Please I need assistance of how to get this done.

     def contextMenuEvent(self, event):
            contextMenu = QMenu(self)
    
            addAction = contextMenu.addAction("Add New")
            editAction = contextMenu.addAction("Edit")
            deleteAction = contextMenu.addAction("Delete")
    
    
            action = contextMenu.exec_(self.mapToGlobal(event.pos()))
    
            if action == editAction:
               
                self.updatePersonID()
                self.Info_Updatesearch()
             
    
            if action == deleteAction:
                self.singleClick_searchDelete()
             
    
            if action == addAction:
                self.ui.stackedWidget.setCurrentWidget(self.ui.personalInformation_page)
    
    J M 2 Replies Last reply 10 Oct 2022, 12:26
    0
    • L LT-K101
      11 Oct 2022, 10:05

      @JonB Please could you direct me on how to achieve this, this is the first time I'm trying this.

      J Offline
      J Offline
      JonB
      wrote on 11 Oct 2022, 10:09 last edited by JonB 10 Nov 2022, 10:10
      #9

      @LT-K101
      I really ought not have to do this, you should know this, it is common sense.

      If you want customContextMenuRequested signal to be raised you have to tell widget to do so when you set up the connection:

      self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
      self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu)
      
      L 1 Reply Last reply 11 Oct 2022, 10:45
      2
      • L LT-K101
        10 Oct 2022, 12:18

        I want a context menu to work with a specific TableWidget but when I click anywhere in the application the context menu pops up. Below is the code I tried, Please I need assistance of how to get this done.

         def contextMenuEvent(self, event):
                contextMenu = QMenu(self)
        
                addAction = contextMenu.addAction("Add New")
                editAction = contextMenu.addAction("Edit")
                deleteAction = contextMenu.addAction("Delete")
        
        
                action = contextMenu.exec_(self.mapToGlobal(event.pos()))
        
                if action == editAction:
                   
                    self.updatePersonID()
                    self.Info_Updatesearch()
                 
        
                if action == deleteAction:
                    self.singleClick_searchDelete()
                 
        
                if action == addAction:
                    self.ui.stackedWidget.setCurrentWidget(self.ui.personalInformation_page)
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 10 Oct 2022, 12:26 last edited by jsulm 10 Oct 2022, 12:27
        #2

        @LT-K101 said in How to create a context menu:

        def contextMenuEvent(self, event)

        I guess this is in your main window (you did not mention this)?
        You need to subclass QTableWidget and override contextMenuEvent there...
        Or do it in main window but check which widget is under the mouse cursor and only show pop-up if it is over your table widget.

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

        1 Reply Last reply
        0
        • L LT-K101
          10 Oct 2022, 12:18

          I want a context menu to work with a specific TableWidget but when I click anywhere in the application the context menu pops up. Below is the code I tried, Please I need assistance of how to get this done.

           def contextMenuEvent(self, event):
                  contextMenu = QMenu(self)
          
                  addAction = contextMenu.addAction("Add New")
                  editAction = contextMenu.addAction("Edit")
                  deleteAction = contextMenu.addAction("Delete")
          
          
                  action = contextMenu.exec_(self.mapToGlobal(event.pos()))
          
                  if action == editAction:
                     
                      self.updatePersonID()
                      self.Info_Updatesearch()
                   
          
                  if action == deleteAction:
                      self.singleClick_searchDelete()
                   
          
                  if action == addAction:
                      self.ui.stackedWidget.setCurrentWidget(self.ui.personalInformation_page)
          
          M Offline
          M Offline
          mpergand
          wrote on 10 Oct 2022, 13:02 last edited by
          #3

          @LT-K101
          Complete example here:
          custommenu_signal.py

          1 Reply Last reply
          2
          • L Offline
            L Offline
            LT-K101
            wrote on 11 Oct 2022, 09:17 last edited by LT-K101 10 Nov 2022, 09:22
            #4

            @jsulm I tried the code below but it does not seem working , I think am doing something wrong.

            self.ui.tableWidget.customContextMenuRequested.connect(openMenu)
            def openMenu(self, position):
                self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
            
                menu = QMenu()
                editAction = menu.addAction('Edit')
            
                action = menu.exec_(self.ui.tableWidget.mapToGlobal(position))
            
                if action == editAction:
                   self.ui.stackedWidget.setCurrentWidget(self.ui.personalInformation_editpage)
            
             self.ui.tableWidget.show()
            
            J 1 Reply Last reply 11 Oct 2022, 09:25
            0
            • L LT-K101
              11 Oct 2022, 09:17

              @jsulm I tried the code below but it does not seem working , I think am doing something wrong.

              self.ui.tableWidget.customContextMenuRequested.connect(openMenu)
              def openMenu(self, position):
                  self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
              
                  menu = QMenu()
                  editAction = menu.addAction('Edit')
              
                  action = menu.exec_(self.ui.tableWidget.mapToGlobal(position))
              
                  if action == editAction:
                     self.ui.stackedWidget.setCurrentWidget(self.ui.personalInformation_editpage)
              
               self.ui.tableWidget.show()
              
              J Offline
              J Offline
              JonB
              wrote on 11 Oct 2022, 09:25 last edited by JonB 10 Nov 2022, 09:25
              #5

              @LT-K101 said in How to create a context menu:

              self.ui.tableWidget.customContextMenuRequested.connect(openMenu)

              You should look at and show the error message!

              openMenu() is a method in a class. So:

              self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu)
              
              L 1 Reply Last reply 11 Oct 2022, 09:54
              0
              • J JonB
                11 Oct 2022, 09:25

                @LT-K101 said in How to create a context menu:

                self.ui.tableWidget.customContextMenuRequested.connect(openMenu)

                You should look at and show the error message!

                openMenu() is a method in a class. So:

                self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu)
                
                L Offline
                L Offline
                LT-K101
                wrote on 11 Oct 2022, 09:54 last edited by
                #6

                @JonB I did correction as you showed self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu) but the contextMenu is still not popping up.

                J 1 Reply Last reply 11 Oct 2022, 09:58
                0
                • L LT-K101
                  11 Oct 2022, 09:54

                  @JonB I did correction as you showed self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu) but the contextMenu is still not popping up.

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Oct 2022, 09:58 last edited by
                  #7

                  @LT-K101
                  Since you set self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu) only inside def openMenu(), how do you expect the self.ui.tableWidget.customContextMenuRequested signal to be raised to call self.openMenu() in the first place?

                  L 1 Reply Last reply 11 Oct 2022, 10:05
                  0
                  • J JonB
                    11 Oct 2022, 09:58

                    @LT-K101
                    Since you set self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu) only inside def openMenu(), how do you expect the self.ui.tableWidget.customContextMenuRequested signal to be raised to call self.openMenu() in the first place?

                    L Offline
                    L Offline
                    LT-K101
                    wrote on 11 Oct 2022, 10:05 last edited by
                    #8

                    @JonB Please could you direct me on how to achieve this, this is the first time I'm trying this.

                    J 1 Reply Last reply 11 Oct 2022, 10:09
                    0
                    • L LT-K101
                      11 Oct 2022, 10:05

                      @JonB Please could you direct me on how to achieve this, this is the first time I'm trying this.

                      J Offline
                      J Offline
                      JonB
                      wrote on 11 Oct 2022, 10:09 last edited by JonB 10 Nov 2022, 10:10
                      #9

                      @LT-K101
                      I really ought not have to do this, you should know this, it is common sense.

                      If you want customContextMenuRequested signal to be raised you have to tell widget to do so when you set up the connection:

                      self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
                      self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu)
                      
                      L 1 Reply Last reply 11 Oct 2022, 10:45
                      2
                      • J JonB
                        11 Oct 2022, 10:09

                        @LT-K101
                        I really ought not have to do this, you should know this, it is common sense.

                        If you want customContextMenuRequested signal to be raised you have to tell widget to do so when you set up the connection:

                        self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
                        self.ui.tableWidget.customContextMenuRequested.connect(self.openMenu)
                        
                        L Offline
                        L Offline
                        LT-K101
                        wrote on 11 Oct 2022, 10:45 last edited by
                        #10

                        @JonB Thanks a lot it worked!!!

                        1 Reply Last reply
                        0

                        7/10

                        11 Oct 2022, 09:58

                        • Login

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