<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Hello,<br><br>I'm a PyQt/Qt newbie who is trying to port some ideas of mine from (Py)Gtk to PyQt and I'm having some conceptual problems. The problem that I am trying to solve is how to add "right click" functionality to a QPushButton that I received e.g. from the designer. I managed to solve this as follows when I inherit the widget myself:<br>
<br><span style="font-family: courier new,monospace;">class MyButton(QtGui.QPushButton):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def __init__(self, parent=None):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> QtGui.QPushButton.__init__(self, parent)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.setText("Press me")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def mousePressEvent(self,event):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if event.button() == QtCore.Qt.LeftButton:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> QtGui.QPushButton.mousePressEvent(self,event)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print "Processing right click event"</span><br style="font-family: courier new,monospace;">
<br>But how would I do the same override in runtime if I all I have is a button that is a QPushButton? I tried doing:<br><br><span style="font-family: courier new,monospace;"> button.mousePressEvent = myMousePressEvent<br style="font-family: courier new,monospace;">
</span>
<span style="font-family: courier new,monospace;"></span><br>But doing that lead to lots of reference problems with the "self" object, since the myMousePressEvent is not in the inheritance chain from QPushButton.<br>
<br>This is actually related to another even more fundamental question, why is the difference between overriding and connecting? I.e. why is "clicked" a signal and "mousePressEvent" an overrided virtual. Why can't they both be signals, in which case the problem I describe wouldn't happen? (That's how it works in Gtk, btw.)<br>
<br>Thanks in advance!<br>Dov<br><br><div></div></div>
</div><br><div style="display: none;" id="divCleekiAttrib"></div></div>