Skip to content

Commit

Permalink
Test the fix for spyder-ide#394
Browse files Browse the repository at this point in the history
  • Loading branch information
StSav012 committed Feb 23, 2023
1 parent f3239aa commit f45ec55
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
4 changes: 4 additions & 0 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def movePositionPatched(
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()
Expand Down
40 changes: 31 additions & 9 deletions 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 @@ -62,14 +62,36 @@ def test_enum_access():
assert QtGui.QImage.Format_Invalid == QtGui.QImage.Format.Format_Invalid


def test_QMouseEvent_pos_functions():
"""Test `QMouseEvent.pos` and related functions obsolete in Qt6"""
assert QtGui.QMouseEvent.pos is not None
assert QtGui.QMouseEvent.x is not None
assert QtGui.QMouseEvent.y is not None
assert QtGui.QMouseEvent.globalPos is not None
assert QtGui.QMouseEvent.globalX is not None
assert QtGui.QMouseEvent.globalY is not None
@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 obsolete in Qt6,
test `QMouseEvent.position` and related functions missing from Qt5"""

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.show()

QtCore.QTimer.singleShot(100, lambda: qtbot.mouseMove(window, QtCore.QPoint(42, 6 * 9)))
QtCore.QTimer.singleShot(200, lambda: qtbot.mouseDClick(window, QtCore.Qt.LeftButton))
QtCore.QTimer.singleShot(300, lambda: window.close())


@pytest.mark.skipif(not (PYSIDE2 or PYSIDE6), reason="PySide{2,6} specific test")
Expand Down

0 comments on commit f45ec55

Please sign in to comment.