Search code examples
pythonpyqtmayapyside2

Create right click menu pyQT Maya


I am currently learning/Converting my Maya.cmds GUI over to PYQT. I have run into some problems learning how to create "popup menus"

Maya pop up menu example on QPushButton

This use to be second nature to me in Maya.cmds but since moving to Qt I am having problems finding any information about this. I would like to add Check boxes, Radial selections and QLineEdit inside this "popup menu".


Solution

  • Here is a working version I have put together, we are using a custom context menu to show our Qmenu with all our menu items.

    class MainWindow(QtWidgets.QDialog):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setWindowTitle("MainWindow")
    
        #   C R E A T E   L A Y O U T
            mainLayout = QtWidgets.QVBoxLayout()
            self.setLayout(mainLayout)
    
        #   C R E A T E   B U T T O N
            self.btn = QtWidgets.QPushButton('Right Click Me!')
            mainLayout.addWidget(self.btn)
    
        #   C O N N E C T   P O P U P   M E N U   T O   O U R   B U T T O N
            self.btn.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            self.btn.customContextMenuRequested.connect(self.showPopup)
    
        #   M E N U   I T E M S
            self.popupMenu = QtWidgets.QMenu()
            self.PBSaveFileCB = self.popupMenu.addAction("Click")
    
    
        #   S H O W   P O P U P   M E N U
        def showPopup(self,position):
             self.popupMenu.exec_(self.btn.mapToGlobal(position))
    
    def showUI():
        ui = MainWindow()
        ui.show()
        return ui
    
    ui = showUI()