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
Servers for Qt installer are currently down

How to create a context menu

Scheduled Pinned Locked Moved Solved Qt for Python
10 Posts 4 Posters 2.1k Views 1 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.
  • L Offline
    L Offline
    LT-K101
    wrote on 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)
    
    jsulmJ M 2 Replies Last reply
    0
    • L LT-K101

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      2
      • L LT-K101

        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)
        
        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #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

          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 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 last edited by LT-K101
            #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()
            
            JonBJ 1 Reply Last reply
            0
            • L LT-K101

              @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()
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              0
              • JonBJ JonB

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

                JonBJ 1 Reply Last reply
                0
                • L LT-K101

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  0
                  • JonBJ JonB

                    @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 last edited by
                    #8

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

                    JonBJ 1 Reply Last reply
                    0
                    • L LT-K101

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #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
                      2
                      • JonBJ JonB

                        @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 last edited by
                        #10

                        @JonB Thanks a lot it worked!!!

                        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