Search code examples
pythonqtpyqtpyqt4signals-slots

User-defined slots for menu pyqt4


According to what I know, Qt Designer is unable to work with signal/slot handling(a slot defined by me). Do you have documentation for user-defined slots for menus?

  1. I use PyQt
  2. Other widgets are able to define user-defined slots

Question: How can I define my own slot for a menu?


Solution

  • If you are asking how to make user-defined slots for menus in Qt, something like this should work:

    Add a QAction to the menu item, and connect the action's triggered signal to a slot.

    class Example(QtGui.QMainWindow):
    
        def __init__(self):
            super(Example, self).__init__()
    
            self.initUI()
    
        def initUI(self):
            testAction = QtGui.QAction(QtGui.QIcon('test.png'), '&Exit', self)
            testAction.triggered.connect(self.runExample)
            menubar = self.menuBar()
            testMenu = menubar.addMenu('&Test')
            testMenu.addAction(testAction)
    
        def runExample(self):
            print "Running example."