[PyQt] Why am I missing a mouseRelease event in QWidget and QGLWidget?

Hans-Peter Jansen hpj at urpla.net
Thu Sep 16 11:43:21 BST 2010


On Thursday 16 September 2010, 10:52:10 HundredsAnd100s wrote:
> I'm new to both PyQt and Qt, so apologies if this is obvious. I'm trying
> to track mouse events with a QGLWidget but it's not working quite like
> expected. I switched to a QWidget to avoid any possible complications but
> the problem remains.
>
> I want to recognize that one or more mouse buttons are being pressed
> simultaneously. This works except for combinations of left and right
> buttons. If, at any time, both those buttons end up pressed, the button I
> release first won't generate a mouseRelease event. The other button will,
> but Qt seems to think the one I released first is still depressed and
> grabs the mouse until I press and release that button again, at which
> point I get a single mouseRelease event.
>
> For instance:
> Press left button --> mousePress: event.button()=left, event.buttons() =
> left
> Press right button --> mousePress: event.button()=right, event.buttons()
> = left | right
> Release right button --> nothing
> Release left button --> mouseRelease: event.button()=left,
> event.buttons()=right
>     ...try to click other things but the mouse is locked...
> Press right button --> nothing
> Release right button --> mouseRelease: event.button()=right,
> event.buttons()=0
>     ...Now I can use the mouse again...
>
> The code works for all three buttons on their own and for any combination
> of left and middle or right and middle buttons. Just not left and right.
> This is under Linux, for what that's worth, and I'm confident I'm not
> simulating a three-button mouse.
>
> I suspect this has something to do with context menus but can't find
> anything in QWidget that has any effect. I overrode event() and noticed
> that left and middle button presses were reported as
> QEvent.MouseButtonPress while right button presses were reported as
> QEvent.ContextMenu, even with the widget's context menu policy set to
> NoContextMenu or PreventContextMenu.
>
> I know I'm missing something, so if someone could point out what it is
> I'd be grateful. Here's the code:
>
> import sys
> import PyQt4.QtCore as core
> import PyQt4.QtGui as gui
>
> class MyWidget(gui.QWidget):
> 	def __init__(self, *args, **kwargs):
> 		gui.QWidget.__init__(self, *args, **kwargs)
> 		self.setAttribute(core.Qt.WA_NoMousePropagation, False)
>
> self.setContextMenuPolicy(core.Qt.ContextMenuPolicy(core.Qt.PreventContex
>tMenu))
>
> 	def mousePressEvent(self, event):
> 		print "Mouse press:",  event.button(), int(event.buttons())
>
> 	def mouseReleaseEvent(self, event):
> 		print "Mouse release:",  event.button(), int(event.buttons())
>
>
> class MainWindow(gui.QMainWindow):
> 	def __init__(self, parent=None):
> 		gui.QWidget.__init__(self, parent)
> 		self.setCentralWidget(MyWidget())
>
>
> app = gui.QApplication(sys.argv)
> main_window = MainWindow()
> main_window.show()
> app.exec_()

Hmm, not here, neither with self.setContextMenuPolicy (indented on 
self.setAttribute level), nor without:

With:
Mouse press: 1 1
Mouse release: 1 0
Mouse press: 4 4
Mouse release: 4 0
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 4 4
Mouse press: 1 5
Mouse release: 4 1
Mouse release: 1 0
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 1 1
Mouse press: 2 3
Mouse release: 1 2
Mouse release: 2 0
Mouse press: 1 1
Mouse release: 1 0
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 1 1
Mouse press: 2 3
Mouse release: 1 2
Mouse release: 2 0
Mouse press: 4 4
Mouse release: 4 0
Mouse press: 1 1
Mouse release: 1 0
Mouse press: 1 1
Mouse press: 4 5
Mouse release: 4 1
Mouse release: 1 0
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 4 4
Mouse press: 2 6
Mouse release: 4 2
Mouse release: 2 0
Mouse press: 1 1
Mouse press: 2 3
Mouse release: 1 2
Mouse release: 2 0

Without:
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 1 1
Mouse release: 1 0
Mouse press: 4 4
Mouse release: 4 0
Mouse press: 1 1
Mouse press: 2 3
Mouse release: 2 1
Mouse release: 1 0
Mouse press: 1 1
Mouse press: 4 5
Mouse release: 4 1
Mouse release: 1 0
Mouse press: 2 2
Mouse press: 4 6
Mouse release: 4 2
Mouse release: 2 0
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 2 2
Mouse press: 4 6
Mouse release: 4 2
Mouse release: 2 0
Mouse press: 2 2
Mouse release: 2 0
Mouse press: 4 4
Mouse press: 2 6
Mouse release: 4 2
Mouse release: 2 0
Mouse press: 1 1
Mouse release: 1 0
Mouse press: 4 4
Mouse press: 1 5
Mouse release: 4 1
Mouse release: 1 0
Mouse press: 1 1
Mouse press: 2 3
Mouse release: 1 2
Mouse release: 2 0

Python version: 2.6
sip version: 4.10.5
Qt4 version: 4.6.3
PyQt4 version: 4.7.4

Pete


More information about the PyQt mailing list