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

HundredsAnd100s canisrogeris at yahoo.com
Thu Sep 16 09:52:10 BST 2010


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.PreventContextMenu))

	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_()
-- 
View this message in context: http://old.nabble.com/Why-am-I-missing-a-mouseRelease-event-in-QWidget-and-QGLWidget--tp29725398p29725398.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list