I need to catch when a User moves the mouse over the GUI, but not when they're holding down the mouse button (which would do something different).
I can't find any conveniant method to do this,
except to periodically find the mouse position and check it to it's previous position...
Which would suck.
The mouseMoveEvent is only called when the mouse is moved whilst the left mouse button is pressed,
unless ofcourse the widget has 'mouse tracking'. Mouse tracking is not an option for me, because the GUI must behave differently when the mouse is moved and the left mouse button is pressed.
Are there any inbuilt methods to do this?
(or just any clever ideas?)
eg:
Is there a way to check if the left mouse button is being pressed at any time?
Or a 'mouse hover' event that can be applied to a QRect (coordinates)?
Muchas gracias.
Windows 7 (32)
python 2.7
PyQt4
The most straightforward way to do this is to install an event filter on qApp:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
widget = QtGui.QWidget(self)
layout = QtGui.QVBoxLayout(widget)
self.edit = QtGui.QLineEdit(self)
self.list = QtGui.QListWidget(self)
layout.addWidget(self.edit)
layout.addWidget(self.list)
self.setCentralWidget(widget)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseMove:
if event.buttons() == QtCore.Qt.NoButton:
pos = event.pos()
self.edit.setText('x: %d, y: %d' % (pos.x(), pos.y()))
else:
pass # do other stuff
return QtGui.QMainWindow.eventFilter(self, source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.installEventFilter(win)
sys.exit(app.exec_())
As people have said, the correct approach seems to be to call setMouseTracking(True) on the widget. What I'd like to add is that, having done this, you can distinguish between a mouse motion and a mouse drag as follows:
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"
call setMouseTracking(True) method first. Then mouseMoveEvent will be fired without any button pressed.
It seems you've misunderstood what mouseTracking does. It only causes mouseMoveEvent to be fired, nothing else. In other words, it's exactly what you need.
Check the event's buttons() to see if any button was pressed:
For mouse move events, this is all buttons that are pressed down.
Related
Using PyQt5 I am viewing an image in a QGraphicsView. I want to be able to zoom in/out while pressing ctrl and using the mouse wheel. I have this working, however if the image is too large, and there are scroll bars, it ignores the zoom functionality until you scroll to the top or bottom.
How can I fix this to where it does not scroll when ctrl is pressed, while allowing it to zoom in/out.
from PyQt5.QtWidgets import QFileDialog, QLineEdit, QWidget, QPushButton, QApplication, QVBoxLayout, QLabel, QGraphicsView, QGraphicsPixmapItem, QGraphicsScene
from PyQt5.QtCore import pyqtSignal, Qt
from pdf2image import convert_from_path
from PIL import ImageQt
import sys
class step1(QWidget):
changeViewSignal = pyqtSignal()
def __init__(self, parent=None):
super(step1, self).__init__(parent)
self.name = QLineEdit(self)
self.fileBtn = QPushButton("Select file", self)
self.nextBtn = QPushButton("Next", self)
self.graphicsView = QGraphicsView()
# self.graphicsView.setFrameShadow(QFrame.Raised)
# self.graphicsView.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContentsOnFirstShow)
self.graphicsView.setHorizontalScrollBarPolicy()
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.name)
self.layout.addWidget(self.fileBtn)
self.layout.addWidget(self.nextBtn)
self.layout.addWidget(self.graphicsView)
self.fileBtn.clicked.connect(self.convert_file)
def wheelEvent(self, event):
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ControlModifier:
self.graphicsView.scrollContentsBy(0,0)
x = event.angleDelta().y() / 120
if x > 0:
self.graphicsView.scale(1.05, 1.05)
elif x < 0:
self.graphicsView.scale(.95, .95)
def convert_file(self):
fname = QFileDialog.getOpenFileName(self, 'Open File', 'c:\\', "PDF Files (*.pdf)")
if len(fname[0]) > 0:
pages = convert_from_path(fname[0])
images = []
qimage = ImageQt.toqpixmap(pages[0])
item = QGraphicsPixmapItem(qimage)
scene = QGraphicsScene(self)
scene.addItem(item)
self.graphicsView.setScene(scene)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = step1()
ex.show()
sys.exit(app.exec_())
The scrolling is first handled by the QGraphicsView before it would be propagated up to the parent widget where you are reimplementing the wheelEvent. This is why the scrolling occurs according to the normal QGraphicsView behavior when it has space to scroll.
A solution is to subclass QGraphicsView and reimplement the wheelEvent there instead.
class GraphicsView(QGraphicsView):
def wheelEvent(self, event):
if event.modifiers() & Qt.ControlModifier:
x = event.angleDelta().y() / 120
if x > 0:
self.scale(1.05, 1.05)
elif x < 0:
self.scale(.95, .95)
else:
super().wheelEvent(event)
Then use the subclass name here:
self.graphicsView = GraphicsView()
Besides the proper solution proposed by alec, there's also the option of using an event filter, which can be useful for UIs created in Designer without the need of using promoted widgets.
The important aspect to keep in mind is that the event filter must be installed on the view's viewport() (the widget in which the contents of the scene are actually rendered and, possibly, scrolled), because that is the widget that will receive the wheel event: input events are always sent to the widget under the mouse (or has keyboard focus)[1], and possibly propagated to their parents if the event is not handled[2].
class step1(QWidget):
def __init__(self, parent=None):
# ...
self.graphicsView.viewport().installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == event.Wheel and event.modifiers() & Qt.ControlModifier:
x = event.angleDelta().y() / 120
if x > 0:
self.graphicsView.scale(1.05, 1.05)
elif x < 0:
self.graphicsView.scale(.95, .95)
return True
return super().eventFilter(source, event)
Returning True means that the viewport has handled the event, and it should not be propagated to the parent; QGraphicsView is based on QAbstractScrollArea, and if a wheel event is not handled by the viewport it will call the base wheelEvent implementation of the viewport's parent (the graphics view), which by default will post the event to the scroll bars. If the filter returns True, it will avoid that propagation, thus preventing scrolling.
Note that you should not use scrollContentsBy unless you really know what you're doing; as the documentation explains: «Calling this function in order to scroll programmatically is an error, use the scroll bars instead».
[1] Mouse events are always sent to the topmost widget under the mouse, unless a modal child window is active, or there is a mouse grabber, which is a widget that has received a mouse button press event but didn't receive a mouse button release event yet, or a widget on which grabMouse() was explicitly called. Keyboard events are always sent to the widget of the active window that has current focus, or the widget on which grabKeyboard() has been explicitly called.
[2] "handled event" can be a confusing concept: it doesn't mean that the widget actually "does" something with the event, nor that it doesn't, no matter if the event then becomes accepted or ignored. A widget could "ignore" an event and still react to it in some way: for instance, you might need to notify the user that the event has been received, but let the parent manage it anyway.
I have a QMainWindow application that has multiple widgets (buttons, labels, etc.) inside it.
How can I get an event when the user presses ANYWHERE of the app?
I tried to customize mousePressEvent() function, but this doesn't accept the event when other widgets (buttons, labels, etc.) are pressed.
Explanation:
The handling of mouse events between the widgets goes from children to parents, that is, if the child does not accept the event (it does not use it) then it will pass the event to the parent. For example, if you press on the QPushButton, it accepts the event and the parent is not notified, unlike QLabel that does not consume it, so the event passes to the parent.
Tools:
On the other hand, there are several methods to listen to events in general, such as:
Override any method like mousePressEvent, keyPressEvent, etc or the event or customEvent method.
Use an eventFilter.
Solution:
An alternative is to apply some previous method to all the widgets but you can discard all of them for the current objective, for example:
In the first and second method it would involve detecting when a widget is added or removed (using QEvent::ChildAdded or QEvent::ChildRemoved).
In the first method it would imply override the methods that many times is impossible.
With the above, the problem is attacked on the widgets side, but there are other alternatives:
Override the notify() method of Q{Core, GUi,}Application, verify that it is a widget and that it belongs to the window, it also implies discriminating if the event has already been consumed.
Listen to the mouse event associated with the window (QWindow).
In this case the most reasonable is the second method.
import sys
from PyQt5 import QtCore, QtWidgets
class MouseObserver(QtCore.QObject):
pressed = QtCore.pyqtSignal(QtCore.QPoint)
released = QtCore.pyqtSignal(QtCore.QPoint)
moved = QtCore.pyqtSignal(QtCore.QPoint)
def __init__(self, window):
super().__init__(window)
self._window = window
self.window.installEventFilter(self)
#property
def window(self):
return self._window
def eventFilter(self, obj, event):
if self.window is obj:
if event.type() == QtCore.QEvent.MouseButtonPress:
self.pressed.emit(event.pos())
elif event.type() == QtCore.QEvent.MouseMove:
self.moved.emit(event.pos())
elif event.type() == QtCore.QEvent.MouseButtonRelease:
self.released.emit(event.pos())
return super().eventFilter(obj, event)
class MainWindow(QtWidgets.QMainWindow):
pass
def main(args):
app = QtWidgets.QApplication(args)
w = MainWindow()
w.show()
mouse_observer = MouseObserver(w.window().windowHandle())
mouse_observer.pressed.connect(lambda pos: print(f"pressed: {pos}"))
mouse_observer.released.connect(lambda pos: print(f"released: {pos}"))
mouse_observer.moved.connect(lambda pos: print(f"moved: {pos}"))
app.exec_()
if __name__ == "__main__":
main(sys.argv)
I'm trying to make videogame macro with pyautogui and pyqt5
this is what I want
Press the button and hold it. (mouse pressed)
Drag somewhere outside the GUI window.
release it. (mouse released)
Get the mouse location when the mouse is released
but I couldn't find a way to connect mouse event with button clicks.
class MyWindow(QDialog, form_class):
def __init__(self):
super().__init__()
self.mouseLocation = {}
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.menuBtn.clicked.connect( lambda: self.addLocation('MenuBtn')) # ButtonEvents
self.traininBtn.pressed.connect( lambda: self.addLocation('TraininBtn'))
self.difficultyBtn.pressed.connect( lambda: self.addLocation('DifficultyBtn'))
self.lonewolfBtn.pressed.connect( lambda: self.addLocation('LonewolfBtn'))
def addLocation(self, name):
self.mouseLocation[name] = [pyautogui.position().x, pyautogui.position().y]
def mouseReleaseEvent(self, e):
print('BUTTON RELEASED')
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
Due to an astronomically atrocious bug on PyQt4, I need to fire a mousePressEvent artificially on a QWebView as a last breath of hope. For some obscure reason, QWebView's linkClicked and urlChanged signals do not pass a QUrl when the clicked link has Javascript involved (It does not work on Youtube videos, for example) and when the link was clicked with the left mouse button. The QUrl can be perfectly accessed when the link was clicked with the middle and right buttons.
class CQWebView(QtWebKit.QWebView):
def mousePressEvent(self, event):
if type(event) == QtGui.QMouseEvent:
if event.button() == QtCore.Qt.LeftButton:
# FIRE A QtCore.Qt.MiddleButton EVENT HERE,
# SO THAT I CAN GET THE BLOODY LINK AFTERWARDS.
elif event.button() == QtCore.Qt.MiddleButton:
self.emit(QtCore.SIGNAL("OPEN_IN_NEW_TAB")) # Example
elif event.button() == QtCore.Qt.RightButton:
self.emit(QtCore.SIGNAL("XXX")) # Example
So, I literally want to "click artificially", click without clicking, just trigger the event of clicking with the middle right button on a link, so that I can catch the QUrl correctly.
You can do it using the QtTest module as well. Set an event filter on your web view and check for left click mouse events and send a middle mouse click
def __init__(...)
self.ui_web_view.installEventFilter(self)
def eventFilter(self, obj, event):
if obj == self.ui_web_view:
if event.type() == QtCore.QEvent.MouseButtonPress:
if event.button() == QtCore.Qt.LeftButton:
print 'Handled'
QtTest.QTest.mouseClick(self.ui_web_view, QtCore.Qt.MiddleButton, QtCore.Qt.NoModifier, event.pos())
return True
return False
I've managed to click with the middle button by clicking with the left button using the sendEvent method from QtGui.QApplication.
class CQWebView(QtWebKit.QWebView):
app = None
def __init__(self, app):
QtWebKit.QWebView.__init__(self)
CQWebView.app = app
def mousePressEvent(self, event):
if type(event) == QtGui.QMouseEvent:
if event.button() == QtCore.Qt.LeftButton:
CQWebView.app.sendEvent(self, QtGui.QMouseEvent(event.type(), event.pos(), QtCore.Qt.MiddleButton, event.buttons(), event.modifiers()))
elif event.button() == QtCore.Qt.MiddleButton:
self.emit(QtCore.SIGNAL("linkClicked(const QUrl&)")) # Problem
elif event.button() == QtCore.Qt.RightButton:
self.emit(QtCore.SIGNAL("XXX")) # Example
I just needed to pass the instance of the QApplication to the instance of the custom QWebView on instantiation:
def compose_tab(self, index):
self.tabs[index].append(CQWebView(self))
This way, I click on the QWebView with the left mouse button and I click artificially on it with the middle button. The problem is that doing so I'm overriding the default behavior of the linkClicked and urlChanged signals from the QWebView. Even though I can emit these signals, I can't have access to the address of the link that was clicked, since by accessing QWebView.url() only gives me the present QUrl, and not the future clicked one.
Really frustrating.
I want to detect the hovering of the mouse on a QPushButton. For that I installed an event filter on my button. However the MouseMove event does not trigger exactly when the mouse is over the button. It seems it is sometimes triggered when I click the button on a location which is different from the previous one. To put it simply:
I move the mouse on the button: nothing happens.
I click: MouseButtonPressed event is triggered.
I move the mouse to another location on the button: nothing happens.
I click again: MouseButtonPressed is triggered, MouseMove too.
I would like to get the MouseMove triggered each time the mouse hovers the button. How do I do?
Here is my code:
import sys
from PyQt4 import QtCore
from PyQt4.QtGui import *
class eventFilterWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
widget = QWidget()
button = QPushButton("Trigger event!")
button.installEventFilter(self)
hbox = QHBoxLayout()
hbox.addWidget(button)
widget.setLayout(hbox)
self.setCentralWidget(widget)
self.show()
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
print "You pressed the button"
return True
elif event.type() == QtCore.QEvent.MouseMove:
print "C'mon! CLick-meeee!!!"
return True
return False
def main():
app = QApplication(sys.argv)
#myWindow = EventsWindow()
window = eventFilterWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
EDIT:
In fact, MouseMove is triggered when the mouse is being moved while the QPushButton is pressed.
I found the answer. I was misled as I was searching an event containing the keyword Mouse in it. The event I was looking for actually is QtCore.QEvent.HoverMove.