[PyQt] How to override mousePressEvent for an existing QPushButton widget

Dov Grobgeld dov.grobgeld at gmail.com
Thu Jul 30 18:10:44 BST 2009


Hi Henning,

Thanks for your replies. (I'm returning the conversation to the pyqt list as
I like using old archives for looking for answers to my own questions. :-) )

First of all, let me describe what I am striving for. A while back I wrote
an application called GemTcl that loads user interfaces built through the
Glade Gui builder. GemTcl lets the user right click on all the buttons,
which popups up a context menu which allows the user to edit the a callback
script that is called when left clicking on the button.

And that's what I wanted to replicate in PyQt.

Regarding contextMenuPolicy, how would you use that. I tried the following,
but it didn't work...
class MyApp(QtGui.QWidget):
    def clicked(self):
        print "Processing left button"

    def context_menu(self):
        print "Processing context menu"

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Icon')

        self.button = MyButton(self)
        self.button.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        self.connect(self.button, QtCore.SIGNAL('clicked()'), self.clicked)
        self.connect(self.button,
QtCore.SIGNAL('customContextMenuRequested()'), self.context_menu)

Thanks,
Dov

2009/7/30 Henning Schröder <henning.schroeder at gmail.com>

> I forgot to mention that every widget also has a
> customContextMenuRequested signal. This gets emitted if you set the
> contextMenuPolicy to Qt.CustomContextMenu.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090730/e6d40601/attachment.html


More information about the PyQt mailing list