Why mouseMoveEvent does nothing in PyQt5 - python

I try to use mouseMoveEvent and mousePressEvent in PyQt5 and Python3.5, but there is nothing when I click my mouse. My code is as following, is there something wrong?
from PyQt5 import QtWidgets, QtGui, QtCore
class Window(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
widget = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(widget)
self.graphicsView = QtWidgets.QGraphicsView()
self.graphicsView.setCursor(QtCore.Qt.CrossCursor)
self.graphicsView.setObjectName("graphicsView")
layout.addWidget(self.graphicsView)
self.setCentralWidget(widget)
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.NoButton:
print("Simple mouse motion")
elif event.buttons() == QtCore.Qt.LeftButton:
print("Left click drag")
elif event.buttons() == QtCore.Qt.RightButton:
print("Right click drag")
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print("Press!")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())

Firstly, you must enable mouse-tracking:
self.graphicsView.setMouseTracking(True)
Then you can either use a subclass of QGraphicsView:
class GraphicsView(QtWidgets.QGraphicsView):
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.NoButton:
print("Simple mouse motion")
elif event.buttons() == QtCore.Qt.LeftButton:
print("Left click drag")
elif event.buttons() == QtCore.Qt.RightButton:
print("Right click drag")
super(GraphicsView, self).mouseMoveEvent(event)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print("Press!")
super(GraphicsView, self).mousePressEvent(event)
Or install an event-filter:
self.graphicsView.viewport().installEventFilter(self)
...
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseMove:
if event.buttons() == QtCore.Qt.NoButton:
print("Simple mouse motion")
elif event.buttons() == QtCore.Qt.LeftButton:
print("Left click drag")
elif event.buttons() == QtCore.Qt.RightButton:
print("Right click drag")
elif event.type() == QtCore.QEvent.MouseButtonPress:
if event.button() == QtCore.Qt.LeftButton:
print("Press!")
return super(Window, self).eventFilter(source, event)

I am sure that your events is handled inside QGraphicsView. You have to read more about event propagation. Try it without adding any additional widgets on top of Window. And do not forget abt MouseTracking property, which is false by default and mouse move events without buttons do not happend at all.
I would recomend to read this article. It's is quite old, but still relevant. Also mouse events in QGraphicsView handled in a different way, read docs for more details.
Sory no code samples since I am C++ developer.

Related

How to detect a mouseButtonPressed event outside a QGraphicsView widget?

I have a QGraphicsView widget with a QGraphicsScene set in it. I wish to detect a mouseButtonPressed event anywhere in my application window outside of the QGraphicsView widget.
I tried to install an eventFilter to the central widget of the application as follows:
self.centralwidget.installEventFilter(self)
My QGraphicsView widget is self.viewStartImg. In my eventFilter method, I have the following:
def eventFilter(self, obj, event):
if obj != self.viewStartImg and event.type() == QEvent.MouseButtonPress:
print('Outside the QGraphicsView')
In my application, when I click inside of the QGraphicsView, I still get 'Outside the QGraphicsView' printed out. I believe that's happening because QGraphicsView is a child of the centralWidget, but I am not sure.
Any alternate method of achieving this functionality is highly appreciated!
There are several ways to achieve this. One way would be to subclass QGraphicsView and override mousePressEvent in the subclass. Another possibility is to install an event filter on the view object. For the first method you would do something like this
from PyQt5 import QtWidgets, QtCore
class MyView(QtWidgets.QGraphicsView):
def mousePressEvent(self, event):
print('mouse pressed inside view')
event.accept()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.central = QtWidgets.QWidget(self)
self.view = MyView(self.central)
self.scene = QtWidgets.QGraphicsScene()
self.view.setScene(self.scene)
hlayout = QtWidgets.QHBoxLayout(self.central)
hlayout.setContentsMargins(50,50,50,50)
hlayout.addWidget(self.view)
self.setCentralWidget(self.central)
def mousePressEvent(self, event):
print('mouse pressed ouside view')
event.accept()
if __name__ == "__main__" :
app = QtWidgets.QApplication([])
win = MyWindow()
win.show()
app.exec()
And for the second one
class MyWindow(QtWidgets.QMainWindow):
money_changed = QtCore.pyqtSignal(int)
def __init__(self):
super().__init__()
self.central = QtWidgets.QWidget(self)
self.view = QtWidgets.QGraphicsView(self.central)
self.scene = QtWidgets.QGraphicsScene()
self.view.setScene(self.scene)
hlayout = QtWidgets.QHBoxLayout(self.central)
hlayout.setContentsMargins(50,50,50,50)
hlayout.addWidget(self.view)
self.setCentralWidget(self.central)
self.view.installEventFilter(self)
def mousePressEvent(self, event):
print('mouse pressed ouside view')
event.accept()
def eventFilter(self, object, event):
if object == self.view and event.type() == QtCore.QEvent.MouseButtonPress:
print('mouse pressed inside view')
return True
return super().eventFilter(object, event)
It may not be exactly what you're looking for, but I recently needed to register when the user clicked outside my widget and used this method:
def event(self, event):
if event == None:
return False
if event.type() == QtCore.QEvent.WindowDeactivate:
print "Clicked outside"
self.main_widget.close()
return super(_ComboBoxPlusDialog, self).event(event)
I used this to close the window, as you can see. So it only registers the outside click once. I used this to get around an issue with Qt.Popup disabling input for the IME.

How to get widgets under a QPaintEvent overlay to register mouse events

I am using an overlay with a QPaintEvent to draw over other widgets. I still need the widgets underneath to register mouse events. When I call raise() with the WA_TransparentForMouseEvents flag, I gain control of the widgets again, but of course, lose the paintevent as it's no longer registering any mouse events. What are my options here?
class Overlay(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Overlay, self).__init__(parent)
self.hotBox = parent
self.resize(self.hotBox.width, self.hotBox.height)
def paintEvent(self, event):
#args: [QEvent]
if any ([self.hotBox.name=="main", self.hotBox.name=="viewport"]):
self.raise_()
self.setWindowFlags(QtCore.Qt.WA_TransparentForMouseEvents)
#Initialize painter
painter = QtGui.QPainter(self)
pen = QtGui.QPen(QtGui.QColor(115, 115, 115), 3, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.setBrush(QtGui.QColor(115, 115, 115))
painter.drawEllipse(self.hotBox.point, 5, 5)
#perform paint
if self.hotBox.mousePosition:
mouseX = self.hotBox.mousePosition.x()
mouseY = self.hotBox.mousePosition.y()
line = QtCore.QLine(mouseX, mouseY, self.hotBox.point.x(), self.hotBox.point.y())
painter.drawLine(line)
painter.drawEllipse(mouseX-5, mouseY-5, 10, 10)
If you want to create an overlay using the events mouseXXXEvent you must use an eventFilter, for this solution I based on this answer of #KubaOber adding more functionalities:
import sys
from PySide2 import QtCore, QtGui, QtWidgets
class Overlay(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Overlay, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
self.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
self.start_line, self.end_line = QtCore.QPoint(), QtCore.QPoint()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.fillRect(self.rect(), QtGui.QColor(80, 80, 255, 128))
if not self.start_line.isNull() and not self.end_line.isNull():
painter.drawLine(self.start_line, self.end_line)
def mousePressEvent(self, event):
self.start_line = event.pos()
self.end_line = event.pos()
self.update()
def mouseMoveEvent(self, event):
self.end_line = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.start_line = QtCore.QPoint()
self.end_line = QtCore.QPoint()
class OverlayFactoryFilter(QtCore.QObject):
def __init__(self, parent=None):
super(OverlayFactoryFilter, self).__init__(parent)
self.m_overlay = None
def setWidget(self, w):
w.installEventFilter(self)
if self.m_overlay is None:
self.m_overlay = Overlay()
self.m_overlay.setParent(w)
def eventFilter(self, obj, event):
if not obj.isWidgetType():
return False
if event.type() == QtCore.QEvent.MouseButtonPress:
self.m_overlay.mousePressEvent(event)
elif event.type() == QtCore.QEvent.MouseButtonRelease:
self.m_overlay.mouseReleaseEvent(event)
elif event.type() == QtCore.QEvent.MouseMove:
self.m_overlay.mouseMoveEvent(event)
elif event.type() == QtCore.QEvent.MouseButtonDblClick:
self.m_overlay.mouseDoubleClickEvent(event)
elif event.type() == QtCore.QEvent.Resize:
if self.m_overlay and self.m_overlay.parentWidget() == obj:
self.m_overlay.resize(obj.size())
elif event.type() == QtCore.QEvent.Show:
self.m_overlay.raise_()
return super(OverlayFactoryFilter, self).eventFilter(obj, event)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
factory = OverlayFactoryFilter()
w = QtWidgets.QWidget()
factory.setWidget(w)
button = QtWidgets.QPushButton("Press me", w)
w.show()
sys.exit(app.exec_())

PyQt5/Python - Multiple keypress events are called with only 1 keypress

So I have the following basic window, my issue is that whenever I press the tab button once it triggers the event twice. Printing "tab" and "keypress" twice. I looked around and all I found about this issue was a C++ answer, I tried to understand the solution but was unable too.
from PyQt5 import QtCore, QtWidgets
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__(self)
# Install the event filter that will be used later to detect key presses
QtWidgets.qApp.installEventFilter(self)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
print("Button")
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
print("keypress")
if event.key() == QtCore.Qt.Key_Escape:
self.close()
if event.key() == QtCore.Qt.Key_Tab:
print("Tab")
pass
return super(ItemPrice, self).eventFilter(obj, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
The eventFilter() method needs a boolean result, or 0/1, to return if an event is relevant or not (the filter part). When you return False the event is not blocked, and will hit his target, this lets the application handle the event in a normal way.
In your example, when an expected key is pressed (this is a relevant event), you need to return 1 or True to "intercept" it and prevent the program to handle it, since you provide your own process. In the other cases, you can call the super method like you did :
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
print("keypress")
if event.key() == QtCore.Qt.Key_Escape:
self.close()
return 1
if event.key() == QtCore.Qt.Key_Tab:
print("Tab")
return 1
return super().eventFilter(obj, event)

Bind canvas to window when canvas is dragged in pyqt

I have an app that sets a matplotlib graph to a FigurCanvas and then adds the FigurCanvas to my AppWindow, I have it set up so the graph is draggable. However when I drag the graph the window it is contained in stays where it is so the graph get dragged off the window. Is there a way to bind the two together so when the graph is moved the window stays with it? Here is the code.
from PyQt4 import QtCore
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class GraphCanvas(FigureCanvas):
def __init__(self):
# The window
self.fig = Figure(figsize=(5, 5), dpi=100)
self.ax1 = self.fig.add_subplot(111)
self.ax1.plot([1,2,3], [1,2,3], linewidth=2, color="#c6463d", label="line1")
FigureCanvas.__init__(self, self.fig)
# drag properties
self.draggable = True
self.dragging_threshold = 5
self.__mousePressPos = None
self.__mouseMovePos = None
def mousePressEvent(self, event):
if self.draggable and event.button() == QtCore.Qt.LeftButton:
self.__mousePressPos = event.globalPos() # global
self.__mouseMovePos = event.globalPos() - self.pos() # local
super(GraphCanvas, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.LeftButton:
globalPos = event.globalPos()
moved = globalPos - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# move when user drag window more than dragging_threshould
diff = globalPos - self.__mouseMovePos
self.move(diff)
self.__mouseMovePos = globalPos - self.pos()
super(GraphCanvas, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
if event.button() == QtCore.Qt.LeftButton:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# do not call click event or so on
event.ignore()
self.__mousePressPos = None
super(GraphCanvas, self).mouseReleaseEvent(event)
''' End Class '''
class AppWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(AppWindow, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
layout = QtGui.QVBoxLayout(self)
cpu_canvas = GraphCanvas()
layout.addWidget(cpu_canvas)
''' End Class'''
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main = AppWindow()
main.show()
sys.exit(app.exec_())
If you want to drag the AppWindow you should register the dragging in this class, instead of the Figure canvas.
Inside figure canvas you can then route the drag events to the AppWindow.
The following should work for you, where I did not change much of the code, just rearanged it, added the parent argument to GraphCanvas and let the dragging functions call their parent's functions.
from PyQt4 import QtCore
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class GraphCanvas(FigureCanvas):
def __init__(self, parent = None):
self.parent = parent
# The window
self.fig = Figure(figsize=(5, 5), dpi=100)
self.ax1 = self.fig.add_subplot(111)
self.ax1.plot([1,2,3], [1,2,3], linewidth=2, color="#c6463d", label="line1")
FigureCanvas.__init__(self, self.fig)
def mousePressEvent(self, event):
self.parent.mousePressEvent(event)
def mouseMoveEvent(self,event):
self.parent.mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.parent.mouseReleaseEvent(event)
''' End Class '''
class AppWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(AppWindow, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
layout = QtGui.QVBoxLayout(self)
cpu_canvas = GraphCanvas(self)
layout.addWidget(cpu_canvas)
self.draggable = True
self.dragging_threshold = 5
self.__mousePressPos = None
self.__mouseMovePos = None
def mousePressEvent(self, event):
if self.draggable and event.button() == QtCore.Qt.LeftButton:
self.__mousePressPos = event.globalPos() # global
self.__mouseMovePos = event.globalPos() - self.pos() # local
super(AppWindow, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.LeftButton:
globalPos = event.globalPos()
moved = globalPos - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# move when user drag window more than dragging_threshould
diff = globalPos - self.__mouseMovePos
self.move(diff)
self.__mouseMovePos = globalPos - self.pos()
super(AppWindow, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.__mousePressPos is not None:
if event.button() == QtCore.Qt.LeftButton:
moved = event.globalPos() - self.__mousePressPos
if moved.manhattanLength() > self.dragging_threshold:
# do not call click event or so on
event.ignore()
self.__mousePressPos = None
super(AppWindow, self).mouseReleaseEvent(event)
''' End Class'''
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main = AppWindow()
main.show()
sys.exit(app.exec_())

How to change minimize event behavior in PyQt or PySide?

I'm developing a Qt application and changed the closing behavior with the closeEvent virtual function this way:
class MainWindow(QMainWindow):
def closeEvent(self, event):
event.ignore()
self.hide()
self.trayicon.showMessage('Running', 'Running in the background.')
This works as expected. If I remove event.ignore() the application quits as expected, everything is fine.
I want to control the minimize event too, so when the user clicks the minimize button on the title bar, I want to move the window instead of minimize.
I cannot use the hideEvent virtual function, because the event will be sent to the window anyway, so this code:
def hideEvent(self, event):
event.ignore()
self.move(0,0)
moves the window to the top left AND then minimize it. event.ignore() has no effect here, so I tried using QtCore.QObject.event this way:
def event(self, event):
if event.type() == QEvent.WindowStateChange:
if self.isMinimized():
event.ignore()
self.move(0,0)
return True
return False
The window moves but minimizes again. What is wrong with this ? How can I override the minimize event completely ?
Try the changeEvent and filter for WindowMinimized events, something like this:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.systemTrayIcon = QtGui.QSystemTrayIcon(self)
self.systemTrayIcon.setIcon(QtGui.QIcon.fromTheme("face-smile"))
self.systemTrayIcon.setVisible(True)
self.systemTrayIcon.activated.connect(self.on_systemTrayIcon_activated)
self.label = QtGui.QLabel(self)
self.label.setText("Minimize me!")
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.label)
#QtCore.pyqtSlot(QtGui.QSystemTrayIcon.ActivationReason)
def on_systemTrayIcon_activated(self, reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
if self.isHidden():
self.show()
else:
self.hide()
def changeEvent(self, event):
if event.type() == QtCore.QEvent.WindowStateChange:
if self.windowState() & QtCore.Qt.WindowMinimized:
event.ignore()
self.close()
return
super(MyWindow, self).changeEvent(event)
def closeEvent(self, event):
event.ignore()
self.hide()
self.systemTrayIcon.showMessage('Running', 'Running in the background.')
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())

Categories