-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(mousepress): replace QTest
mouse actions
#429
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,16 +1,16 @@ | ||||||
import contextlib | ||||||
import weakref | ||||||
import warnings | ||||||
import weakref | ||||||
|
||||||
from pytestqt.exceptions import TimeoutError | ||||||
from pytestqt.qt_compat import qt_api | ||||||
from pytestqt.wait_signal import ( | ||||||
SignalBlocker, | ||||||
MultiSignalBlocker, | ||||||
SignalEmittedSpy, | ||||||
SignalEmittedError, | ||||||
CallbackBlocker, | ||||||
CallbackCalledTwiceError, | ||||||
MultiSignalBlocker, | ||||||
SignalBlocker, | ||||||
SignalEmittedError, | ||||||
SignalEmittedSpy, | ||||||
) | ||||||
|
||||||
|
||||||
|
@@ -636,25 +636,85 @@ def keyToAscii(key): | |||||
raise NotImplementedError("This method isn't available on PyQt5.") | ||||||
qt_api.QtTest.QTest.keyToAscii(key) | ||||||
|
||||||
@staticmethod | ||||||
def mouseClick(*args, **kwargs): | ||||||
qt_api.QtTest.QTest.mouseClick(*args, **kwargs) | ||||||
|
||||||
@staticmethod | ||||||
def mouseDClick(*args, **kwargs): | ||||||
qt_api.QtTest.QTest.mouseDClick(*args, **kwargs) | ||||||
|
||||||
@staticmethod | ||||||
def mouseMove(*args, **kwargs): | ||||||
qt_api.QtTest.QTest.mouseMove(*args, **kwargs) | ||||||
|
||||||
@staticmethod | ||||||
def mousePress(*args, **kwargs): | ||||||
qt_api.QtTest.QTest.mousePress(*args, **kwargs) | ||||||
|
||||||
@staticmethod | ||||||
def mouseRelease(*args, **kwargs): | ||||||
qt_api.QtTest.QTest.mouseRelease(*args, **kwargs) | ||||||
def mouseClick(self, widget, button, pos=None, modifiers=None): | ||||||
if pos is None: | ||||||
pos = widget.rect().center() | ||||||
self.mouseMove(widget, pos) | ||||||
self.mousePress(widget, button, pos, modifiers) | ||||||
self.mouseRelease(widget, button, pos, modifiers) | ||||||
|
||||||
def mouseDClick(self, widget, button, pos=None, modifiers=None): | ||||||
if pos is None: | ||||||
pos = widget.rect().center() | ||||||
self.mouseClick(widget, button, pos, modifiers) | ||||||
self.mouseClick(widget, button, pos, modifiers) | ||||||
|
||||||
def mouseDrag(self, widget, button, pos1, pos2, modifiers=None): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is new so we should have a docstring and a test. |
||||||
self.mouseMove(widget, pos1) | ||||||
self.mousePress(widget, pos1, button, modifiers) | ||||||
self.mouseMove(widget, pos2, button, modifiers) | ||||||
self.mouseRelease(widget, pos2, button, modifiers) | ||||||
|
||||||
def mouseMove(self, widget, pos, modifiers=None): | ||||||
if isinstance(widget, qt_api.QtWidgets.QGraphicsView): | ||||||
widget = widget.viewport() | ||||||
Comment on lines
+659
to
+660
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Suggested change
|
||||||
if modifiers is None: | ||||||
modifiers = qt_api.QtCore.Qt.KeyboardModifier.NoModifier | ||||||
if isinstance(pos, qt_api.QtCore.QPoint): | ||||||
pos = qt_api.QtCore.QPointF(pos) # PyQt6 requires `QPointF` | ||||||
buttons = qt_api.QtCore.Qt.MouseButton.NoButton | ||||||
# `QMouseEvent` does not accept keyword arguments. Results in `TypeError: | ||||||
# not enough arguments`. Use positional instead. | ||||||
event = qt_api.QtGui.QMouseEvent( | ||||||
qt_api.QtCore.QEvent.Type.MouseMove, | ||||||
pos, | ||||||
qt_api.QtCore.Qt.MouseButton.NoButton, | ||||||
buttons, | ||||||
modifiers, | ||||||
) | ||||||
qt_api.QtWidgets.QApplication.sendEvent(widget, event) | ||||||
|
||||||
def mousePress(self, widget, button, pos=None, modifiers=None): | ||||||
if isinstance(widget, qt_api.QtWidgets.QGraphicsView): | ||||||
widget = widget.viewport() | ||||||
Comment on lines
+678
to
+679
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here.
Suggested change
|
||||||
if modifiers is None: | ||||||
modifiers = qt_api.QtCore.Qt.KeyboardModifier.NoModifier | ||||||
if pos is None: | ||||||
pos = widget.rect().center() | ||||||
if isinstance(pos, qt_api.QtCore.QPoint): | ||||||
pos = qt_api.QtCore.QPointF(pos) # PyQt6 requires `QPointF` | ||||||
buttons = qt_api.QtCore.Qt.MouseButton.NoButton | ||||||
# `QMouseEvent` does not accept keyword arguments. Results in `TypeError: | ||||||
# not enough arguments`. Use positional instead. | ||||||
event = qt_api.QtGui.QMouseEvent( | ||||||
qt_api.QtCore.QEvent.Type.MouseButtonPress, | ||||||
pos, | ||||||
button, | ||||||
buttons, | ||||||
modifiers, | ||||||
) | ||||||
qt_api.QtWidgets.QApplication.sendEvent(widget, event) | ||||||
|
||||||
def mouseRelease(self, widget, button, pos=None, modifiers=None): | ||||||
if isinstance(widget, qt_api.QtWidgets.QGraphicsView): | ||||||
widget = widget.viewport() | ||||||
Comment on lines
+699
to
+700
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same.
Suggested change
|
||||||
if modifiers is None: | ||||||
modifiers = qt_api.QtCore.Qt.KeyboardModifier.NoModifier | ||||||
if pos is None: | ||||||
pos = widget.rect().center() | ||||||
if isinstance(pos, qt_api.QtCore.QPoint): | ||||||
pos = qt_api.QtCore.QPointF(pos) # PyQt6 requires `QPointF` | ||||||
buttons = qt_api.QtCore.Qt.MouseButton.NoButton | ||||||
# `QMouseEvent` does not accept keyword arguments. Results in `TypeError: | ||||||
# not enough arguments`. Use positional instead. | ||||||
event = qt_api.QtGui.QMouseEvent( | ||||||
qt_api.QtCore.QEvent.Type.MouseButtonRelease, | ||||||
pos, | ||||||
button, | ||||||
buttons, | ||||||
modifiers, | ||||||
) | ||||||
qt_api.QtWidgets.QApplication.sendEvent(widget, event) | ||||||
|
||||||
|
||||||
# provide easy access to exceptions to qtbot fixtures | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import weakref | ||
|
||
import pytest | ||
|
||
from pytestqt import qt_compat | ||
from pytestqt.qt_compat import qt_api | ||
|
||
|
@@ -165,17 +164,19 @@ def extract(mouse_event): | |
|
||
event_recorder.registerEvent(qt_api.QtGui.QMouseEvent, extract) | ||
|
||
qtbot.mousePress(event_recorder, qt_api.QtCore.Qt.MouseButton.LeftButton) | ||
qtbot.mousePress( | ||
widget=event_recorder, button=qt_api.QtCore.Qt.MouseButton.LeftButton | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to make sure: these changes to the tests are not required, are they? |
||
) | ||
assert event_recorder.event_data == ( | ||
qt_api.QtCore.QEvent.Type.MouseButtonPress, | ||
qt_api.QtCore.Qt.MouseButton.LeftButton, | ||
qt_api.QtCore.Qt.KeyboardModifier.NoModifier, | ||
) | ||
|
||
qtbot.mousePress( | ||
event_recorder, | ||
qt_api.QtCore.Qt.MouseButton.RightButton, | ||
qt_api.QtCore.Qt.KeyboardModifier.AltModifier, | ||
widget=event_recorder, | ||
button=qt_api.QtCore.Qt.MouseButton.RightButton, | ||
modifiers=qt_api.QtCore.Qt.KeyboardModifier.AltModifier, | ||
) | ||
assert event_recorder.event_data == ( | ||
qt_api.QtCore.QEvent.Type.MouseButtonPress, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right: if I issue a
mouseClick
, I don't expect amouseMove
to be issued.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair:
mousePress
andmouseRelease
each takepos
as an argument, so they should click the right spot without moving the cursor.