4

im just a beginner in PyQT. and im not sure if my thread title is the correct thing to put for my problem.

im having a problem creating a popmenu on a Qpushbutton. based on the doc of QT docs

i need to make a QPushButton.setMenu (self, QMenu menu)

but i really dont know where to start.. i cant find a sample on how to use this. please help me making one.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Katherina
  • 2,153
  • 7
  • 26
  • 34

1 Answers1

11

The basic idea is that you first have to create a QMenu, then use the setMenu method to attach it to your push button. If you look at the QMenu documentation, you'll see that there is a method called addAction that will add menu items to your newly created QMenu. addAction is overloaded, so there are a lot of different ways to call it. You can use icons in your menu, specify keyboard shortcuts and other things. To keep things simple though, let's just add a menu item and give it a method to call if that item is selected.

from PyQt4 import QtGui, QtCore
import sys


class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        pushbutton = QtGui.QPushButton('Popup Button')
        menu = QtGui.QMenu()
        menu.addAction('This is Action 1', self.Action1)
        menu.addAction('This is Action 2', self.Action2)
        pushbutton.setMenu(menu)
        self.setCentralWidget(pushbutton)

    def Action1(self):
        print 'You selected Action 1'

    def Action2(self):
        print 'You selected Action 2'


if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    app.exec_()

Here we've created a push button (creatively named pushbutton). We then create a menu (again creatively named menu) using QtGui.QMenu(). The actions are created by calling addAction and giving it a string that will be used as the menu item text and a method (self.Action1 or self.Action2) that will be called if that menu item is selected. Then we call the setMenu method of pushbutton to assign our menu to it. When you run it and select an item, you should see text printed corresponding to the selected item.

That's the basic idea. You can look through the QMenu docs to get a better idea of the functionality of QMenu.

Stephen Terry
  • 6,169
  • 1
  • 28
  • 31
  • Hi Stephen, this is really what i need for my studies. thank you soo much. without this kind of examples its hard for me to understand.. thanks again – Katherina Aug 02 '11 at 02:27
  • 2
    thank you for the answer Stephen. I'd just like to point out one caveat - As of Qt 5.3.2, you need the `menu.addAction`s to be done before calling `setMenu` - otherwise the menu that is popped up is of an incorrect size (and shows the added items partially). The other option is to call `setMenu(menu())` just before showing the menu. – Fox Feb 02 '15 at 12:00
  • 1
    It looks like the QMenu should be saved somewhere because "Ownership of the menu is not transferred to the push button" (from the docs). Without saving the menu, it didn't work as expected for me. – ababak Oct 13 '20 at 15:18