diff --git a/Qt.py b/Qt.py index b5c2b431..4157a09b 100644 --- a/Qt.py +++ b/Qt.py @@ -26,6 +26,7 @@ def _pyqt5(): import PyQt5.Qt + from PyQt5 import QtWidgets # Remap PyQt5.QtCore.Signal = PyQt5.QtCore.pyqtSignal @@ -39,6 +40,16 @@ def _pyqt5(): PyQt5.__qt_version__ = PyQt5.QtCore.QT_VERSION_STR PyQt5.load_ui = pyqt5_load_ui + # provide mocked UnicodeUTF8 For backward compatibility + QtWidgets.QApplication.UnicodeUTF8 = -1 + + old_translate_fn = QtWidgets.QApplication.translate + + def translate(context, key, disambiguation=None, encoding=None, n=0): + return old_translate_fn(context, key, disambiguation, n) + + QtWidgets.QApplication.translate = staticmethod(translate) + return PyQt5 @@ -89,7 +100,7 @@ def _pyqt4(): def _pyside2(): import PySide2 - from PySide2 import QtGui, QtCore + from PySide2 import QtGui, QtCore, QtWidgets # Remap QtCore.QStringListModel = QtGui.QStringListModel @@ -101,6 +112,16 @@ def _pyside2(): PySide2.__qt_version__ = PySide2.QtCore.qVersion() PySide2.load_ui = pyside2_load_ui + # provide mocked UnicodeUTF8 For backward compatibility + QtWidgets.QApplication.UnicodeUTF8 = -1 + + old_translate_fn = QtWidgets.QApplication.translate + + def translate(context, key, disambiguation=None, encoding=None, n=0): + return old_translate_fn(context, key, disambiguation, n) + + QtWidgets.QApplication.translate = staticmethod(translate) + return PySide2 @@ -204,13 +225,11 @@ def _init(): this has executed. """ - preferred = os.getenv("QT_PREFERRED_BINDING") verbose = os.getenv("QT_VERBOSE") is not None bindings = (_pyside2, _pyqt5, _pyside, _pyqt4) if preferred: - # Internal flag (used in installer) if preferred == "None": sys.modules[__name__].__wrapper_version__ = __version__ diff --git a/tests.py b/tests.py index 887f1346..2b1bfbc4 100644 --- a/tests.py +++ b/tests.py @@ -208,3 +208,104 @@ def test_sip_api_already_set(): import sip sip.setapi("QString", 1) assert_raises(ImportError, __import__, "Qt") + + +def test_translate_and_UnicodeUTF8_in_pyside(): + _str_ = str + if PYTHON == 2: + _str_ = basestring + + with pyside(): + from Qt import QtWidgets + + # this does exist in PySide + assert QtWidgets.QApplication.UnicodeUTF8 is not None + + # use patched method with old arguments + result = QtWidgets.QApplication.translate( + u'Href_Gui', + u'Text', + u'None', + QtWidgets.QApplication.UnicodeUTF8 + ) + assert isinstance(result, _str_) + + +def test_translate_and_UnicodeUTF8_in_pyqt4(): + _str_ = str + if PYTHON == 2: + _str_ = basestring + + with pyqt4(): + from Qt import QtWidgets + + # this does exist in PyQt4 + assert QtWidgets.QApplication.UnicodeUTF8 is not None + + # use patched method with old arguments + result = QtWidgets.QApplication.translate( + u'Href_Gui', + u'Text', + u'None', + QtWidgets.QApplication.UnicodeUTF8 + ) + + assert isinstance(result, _str_) + + +def test_translate_and_UnicodeUTF8_in_pyside2(): + _str_ = str + if PYTHON == 2: + _str_ = basestring + + with pyside2(): + from Qt import QtWidgets + + # this does not exist in PySide2 by default + assert QtWidgets.QApplication.UnicodeUTF8 is -1 + + # use patched method with old arguments + result = QtWidgets.QApplication.translate( + u'Href_Gui', + u'Text', + u'None', + QtWidgets.QApplication.UnicodeUTF8 + ) + assert isinstance(result, _str_) + + # use patched method with new arguments + result = QtWidgets.QApplication.translate( + u'SomeText', + u'Form', + u'None' + ) + assert isinstance(result, _str_) + + +def test_translate_and_UnicodeUTF8_in_PyQt5(): + _str_ = str + if PYTHON == 2: + _str_ = basestring + + with pyqt5(): + from Qt import QtWidgets + + # this does not exist in PyQt5 by default + assert QtWidgets.QApplication.UnicodeUTF8 is -1 + + # use patched method with old arguments + result = QtWidgets.QApplication.translate( + u'SomeText', + u'Form', + u'None', + QtWidgets.QApplication.UnicodeUTF8 + ) + assert isinstance(result, _str_) + + # use patched method with new arguments + result = QtWidgets.QApplication.translate( + u'SomeText', + u'Form', + u'None' + ) + assert isinstance(result, _str_)