Skip to content

Commit

Permalink
PR: Add mappings for QMouseEvent methods (#408)
Browse files Browse the repository at this point in the history
* Might close #394
* Rephrase a comment, as @CAM-Gerlach suggested
* Test the fix for #394
* Format the docstring as suggested by @CAM-Gerlach
* Ensure the created window is of sufficient size to point at
* Don't wait before moving and clicking the mouse.
  `QMainWindow.show()` finishes when the window appears. So, no extra waiting needed.
* Don't close the window at the end

Co-authored-by: C.A.M. Gerlach <[email protected]>
  • Loading branch information
StSav012 and CAM-Gerlach authored Feb 23, 2023
1 parent 6f7e1e7 commit a112bcf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,16 @@ def movePositionPatched(
) -> bool:
return movePosition(self, operation, mode, n)
QTextCursor.movePosition = movePositionPatched

# Fix https://github.com/spyder-ide/qtpy/issues/394
if PYQT5 or PYSIDE2:
from qtpy.QtCore import QPointF as __QPointF
QMouseEvent.position = lambda self: __QPointF(float(self.x()), float(self.y()))
QMouseEvent.globalPosition = lambda self: __QPointF(float(self.globalX()), float(self.globalY()))
if PYQT6 or PYSIDE6:
QMouseEvent.pos = lambda self: self.position().toPoint()
QMouseEvent.x = lambda self: self.position().toPoint().x()
QMouseEvent.y = lambda self: self.position().toPoint().y()
QMouseEvent.globalPos = lambda self: self.globalPosition().toPoint()
QMouseEvent.globalX = lambda self: self.globalPosition().toPoint().x()
QMouseEvent.globalY = lambda self: self.globalPosition().toPoint().y()
37 changes: 36 additions & 1 deletion qtpy/tests/test_qtgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from qtpy import PYQT5, PYQT_VERSION, PYSIDE2, PYSIDE6, QtGui
from qtpy import PYQT5, PYQT_VERSION, PYSIDE2, PYSIDE6, QtCore, QtGui, QtWidgets
from qtpy.tests.utils import not_using_conda


Expand Down Expand Up @@ -61,6 +61,41 @@ def test_enum_access():
assert QtGui.QIcon.Normal == QtGui.QIcon.Mode.Normal
assert QtGui.QImage.Format_Invalid == QtGui.QImage.Format.Format_Invalid


@pytest.mark.skipif(
sys.platform.startswith('linux') and not_using_conda(),
reason="Fatal Python error: Aborted on Linux CI when not using conda")
@pytest.mark.skipif(
sys.platform == 'darwin' and sys.version_info[:2] == (3, 7),
reason="Stalls on macOS CI with Python 3.7")
def test_QMouseEvent_pos_functions(qtbot):
"""
Test `QMouseEvent.pos` and related functions removed in Qt 6,
and `QMouseEvent.position`, etc., missing from Qt 5.
"""

class Window(QtWidgets.QMainWindow):
def mouseDoubleClickEvent(self, event: QtGui.QMouseEvent) -> None:
assert event.globalPos() - event.pos() == self.mapToParent(QtCore.QPoint(0, 0))
assert event.pos().x() == event.x()
assert event.pos().y() == event.y()
assert event.globalPos().x() == event.globalX()
assert event.globalPos().y() == event.globalY()
assert event.position().x() == event.pos().x()
assert event.position().y() == event.pos().y()
assert event.globalPosition().x() == event.globalPos().x()
assert event.globalPosition().y() == event.globalPos().y()

event.accept()

window = Window()
window.setMinimumSize(320, 240) # ensure the window is of sufficient size
window.show()

qtbot.mouseMove(window, QtCore.QPoint(42, 6 * 9))
qtbot.mouseDClick(window, QtCore.Qt.LeftButton)


@pytest.mark.skipif(not (PYSIDE2 or PYSIDE6), reason="PySide{2,6} specific test")
def test_qtextcursor_moveposition():
"""Test monkeypatched QTextCursor.movePosition"""
Expand Down

0 comments on commit a112bcf

Please sign in to comment.