Skip to content

Commit

Permalink
PR: Improve compatibility for QtWidgets and QtGui modules between…
Browse files Browse the repository at this point in the history
… Qt5 and Qt6 bindings (#410)
  • Loading branch information
dalthviz authored Mar 21, 2023
2 parents c67b149 + 5c6dd9c commit 84d3643
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
11 changes: 11 additions & 0 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

if PYQT5:
from PyQt5.QtGui import *

# Backport items moved to QtGui in Qt6
from PyQt5.QtWidgets import QAction, QActionGroup, QFileSystemModel, QShortcut, QUndoCommand
elif PYQT6:
from PyQt6 import QtGui
from PyQt6.QtGui import *
Expand All @@ -30,12 +33,20 @@
del QtGui
elif PYSIDE2:
from PySide2.QtGui import *

# Backport items moved to QtGui in Qt6
from PySide2.QtWidgets import QAction, QActionGroup, QFileSystemModel, QShortcut, QUndoCommand

if hasattr(QFontMetrics, 'horizontalAdvance'):
# Needed to prevent raising a DeprecationWarning when using QFontMetrics.width
QFontMetrics.width = lambda self, *args, **kwargs: self.horizontalAdvance(*args, **kwargs)
elif PYSIDE6:
from PySide6.QtGui import *
from PySide6.QtOpenGL import *

# Backport `QFileSystemModel` moved to QtGui in Qt6
from PySide6.QtWidgets import QFileSystemModel

QFontMetrics.width = lambda self, *args, **kwargs: self.horizontalAdvance(*args, **kwargs)
QFontMetricsF.width = lambda self, *args, **kwargs: self.horizontalAdvance(*args, **kwargs)

Expand Down
9 changes: 9 additions & 0 deletions qtpy/tests/test_qtgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def test_qguiapplication_functions():
assert QtGui.QGuiApplication.exec_ is not None


def test_what_moved_to_qtgui_in_qt6():
"""Test what has been moved to QtGui in Qt6"""
assert QtGui.QAction is not None
assert QtGui.QActionGroup is not None
assert QtGui.QFileSystemModel is not None
assert QtGui.QShortcut is not None
assert QtGui.QUndoCommand is not None


@pytest.mark.skipif(
sys.platform.startswith('linux') and not_using_conda(),
reason="Segmentation fault/Aborted on Linux CI when not using conda")
Expand Down
10 changes: 7 additions & 3 deletions qtpy/tests/test_qtwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ def test_qlineedit_functions():
assert QtWidgets.QLineEdit.getTextMargins


def test_qundocommand_object():
"""Test object aliasing for QUndoCommand"""
assert QtWidgets.QUndoCommand
def test_what_moved_to_qtgui_in_qt6():
"""Test that we move back what has been moved to QtGui in Qt6"""
assert QtWidgets.QAction is not None
assert QtWidgets.QActionGroup is not None
assert QtWidgets.QFileSystemModel is not None
assert QtWidgets.QShortcut is not None
assert QtWidgets.QUndoCommand is not None


@pytest.mark.skipif(
Expand Down

0 comments on commit 84d3643

Please sign in to comment.