Skip to content
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

Missing QUndoStack import mapping (Qt5 QtWidgets vs Qt6 QtGui ) #490

Open
th3w1zard1 opened this issue May 26, 2024 · 2 comments
Open

Missing QUndoStack import mapping (Qt5 QtWidgets vs Qt6 QtGui ) #490

th3w1zard1 opened this issue May 26, 2024 · 2 comments

Comments

@th3w1zard1
Copy link

th3w1zard1 commented May 26, 2024

Have ran into an issue where I'm not getting expected imports from the expected module.

import qtpy
if qtpy.API_NAME in ("PyQt5", "PySide2"):
    from qtpy.QtWidgets import QUndoStack
elif qtpy.API_NAME in ("PyQt6", "PySide6"):
    from qtpy.QtGui import QUndoStack
else:
    raise ValueError(f"Invalid QT_API: '{qtpy.API_NAME}'")

is this something qtpy could explain/fix?

@th3w1zard1
Copy link
Author

th3w1zard1 commented May 26, 2024

I was going to create a new issue for each but it seems I have a bunch of small ones:

Issue 1:

        self._proxyModel = QSortFilterProxyModel(self)
        self._proxyModel.setSourceModel(self)
        self._proxyModel.setRecursiveFilteringEnabled(True)  # type: ignore[arg-type]
        self._proxyModel.setFilterCaseSensitivity(False if qtpy.API_NAME in ("PyQt5", "PySide2") else QtCore.Qt.CaseInsensitive)  # type: ignore[arg-type]

raises an exception and includes the phrase 'incorrect signature' in the message.

Issue 2:

if qtpy.API_NAME in ("PyQt6", "PySide6"):
    ...
else:
    from qtpy.QtWidgets import QDesktopWidget

Would love to know the correct place to import QDesktopWidget from in the above?

Issue 3:

def SomeWindow(QMainWindow):
    def __init__(*args, **kwargs):
        if qtpy.API_NAME in {"PySide2", "PyQt5"}:
            self.player.error.connect(lambda _=None: self.handleError())
        else:
            self.player.errorOccurred.connect(lambda *args, **kwargs: self.handleError(*args, **kwargs))
        if qtpy.API_NAME in ["PyQt5", "PySide2"]:
            # PyQt5 and PySide2 code path
            from qtpy.QtMultimedia import QMediaContent

            def set_media(data: bytes | None):
                if data:
                    self.buffer = QBuffer(self)
                    self.buffer.setData(data)
                    self.buffer.open(QIODevice.ReadOnly)
                    self.player.setMedia(QMediaContent(), self.buffer)
                    QtCore.QTimer.singleShot(0, self.player.play)
                else:
                    self.blinkWindow()

        elif qtpy.API_NAME in ["PyQt6", "PySide6"]:
            # PyQt6 and PySide6 code path
            def set_media(data: bytes | None):
                if data:
                    self.tempFile = NamedTemporaryFile(delete=False, suffix=".wav")
                    self.tempFile.write(data)
                    self.tempFile.close()
                    print(f"Wrote audioplayer audio data to '{self.tempFile.name}'")
                    self.player.setSource(QUrl.fromLocalFile(self.tempFile.name))
                else:
                    self.blinkWindow()

Spent hours on this but I cannot figure out how to get the media player working correctly in qt6. Works fine in pyside2/pyqt5.

Issue 4:

if qtpy.API_NAME in ("PyQt6", "PySide6"):
    from qtpy.QtCore import QRegularExpression as QRegExp
    from qtpy.QtGui import QRegularExpressionValidator as QRegExpValidator
else:
    from qtpy.QtCore import QRegExp
    from qtpy.QtGui import QRegExpValidator

    def resRefValidator(self) -> QRegExpValidator:
        return QRegExpValidator(QRegExp(r"^[a-zA-Z0-9_]*$"))

I'm 70% certain there's a bigger change here than the names that is required. How exactly do I unify my use of q regex objects?

@dalthviz
Copy link
Member

Hi @th3w1zard1 and sorry for such a late response! Regarding your initial compatibilty issue related with QUndoStack I would say adding over QtPy the missing import at

qtpy/qtpy/QtWidgets.py

Lines 37 to 44 in 0f7b181

elif PYQT6:
from PyQt6 import QtWidgets
from PyQt6.QtGui import (
QActionGroup,
QFileSystemModel,
QShortcut,
QUndoCommand,
)

qtpy/qtpy/QtWidgets.py

Lines 113 to 114 in 0f7b181

elif PYSIDE6:
from PySide6.QtGui import QActionGroup, QShortcut, QUndoCommand

qtpy/qtpy/QtGui.py

Lines 54 to 64 in 0f7b181

if PYQT5:
from PyQt5.QtGui import *
# Backport items moved to QtGui in Qt6
from PyQt5.QtWidgets import (
QAction,
QActionGroup,
QFileSystemModel,
QShortcut,
QUndoCommand,
)

and at

qtpy/qtpy/QtGui.py

Lines 109 to 119 in 0f7b181

elif PYSIDE2:
from PySide2.QtGui import *
# Backport items moved to QtGui in Qt6
from PySide2.QtWidgets import (
QAction,
QActionGroup,
QFileSystemModel,
QShortcut,
QUndoCommand,
)

should be enough to enable usage either by the Qt5 or Qt6 import syntax: from qtpy.QtWidgets import QUndoStack or from qtpy.QtGui import QUndoStack

Related with the other issues you mention, we would need to give a more in depth check to them but the one I know an answer already its issue 2 which basically is that QDesktopWidget was removed over Qt6 (it was marked as deprecated already over Qt5). For more info about alternatives for its functionality you can check https://doc.qt.io/qt-6/widgets-changes-qt6.html#qdesktopwidget-and-qapplication-desktop

Let us know if you would like to help with the QUndoStack import and if the info above helps!

@dalthviz dalthviz changed the title Please unify QUndoStack? Missing QUndoStack import mapping (Qt5 QtWidgets vs Qt6 QtGui ) Aug 21, 2024
@dalthviz dalthviz added this to the v2.4.2 milestone Aug 21, 2024
@ccordoba12 ccordoba12 modified the milestones: v2.4.2, v2.4.3 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants