Skip to content

Commit 952b91d

Browse files
authored
Merge pull request #217 from tbttfox/loadUi-layout-fix
Ignore the default empty layout objects when loading a .ui file
2 parents cb4f3e7 + 9531a0f commit 952b91d

File tree

3 files changed

+287
-33
lines changed

3 files changed

+287
-33
lines changed

CAVEATS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ There are cases where Qt.py is not handling incompatibility issues.
1010
- [QtWidgets.QHeaderView.setResizeMode](#qtwidgetsqheaderviewsetresizemode)
1111
- [QtWidgets.qApp](#qtwidgetsqapp)
1212
- [QtCompat.wrapInstance](#qtcompatwrapinstance)
13+
- [QtCore.qInstallMessageHandler](#qtcoreqinstallmessagehandler)
1314

1415
<br>
1516
<br>

Qt.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import shutil
4444

4545

46-
__version__ = "1.1.0.b8"
46+
__version__ = "1.1.0.b9"
4747

4848
# Enable support for `from Qt import *`
4949
__all__ = []
@@ -706,7 +706,6 @@
706706
"QtCore.Signal": "QtCore.Signal",
707707
"QtCore.Slot": "QtCore.Slot",
708708
"QtGui.QItemSelectionRange": "QtCore.QItemSelectionRange",
709-
710709
},
711710
"PyQt4": {
712711
"QtGui.QAbstractProxyModel": "QtCore.QAbstractProxyModel",
@@ -1028,6 +1027,7 @@ def _pyside2():
10281027

10291028
if hasattr(Qt, "_QtCore"):
10301029
Qt.__qt_version__ = Qt._QtCore.qVersion()
1030+
Qt.QtCompat.qInstallMessageHandler = _qInstallMessageHandler
10311031
Qt.QtCompat.translate = Qt._QtCore.QCoreApplication.translate
10321032

10331033
if hasattr(Qt, "_QtWidgets"):
@@ -1079,6 +1079,7 @@ def _pyside():
10791079
if hasattr(Qt, "_QtCore"):
10801080
Qt.__qt_version__ = Qt._QtCore.qVersion()
10811081
QCoreApplication = Qt._QtCore.QCoreApplication
1082+
Qt.QtCompat.qInstallMessageHandler = _qInstallMessageHandler
10821083
Qt.QtCompat.translate = (
10831084
lambda context, sourceText, disambiguation, n:
10841085
QCoreApplication.translate(
@@ -1118,6 +1119,7 @@ def _pyqt5():
11181119
if hasattr(Qt, "_QtCore"):
11191120
Qt.__binding_version__ = Qt._QtCore.PYQT_VERSION_STR
11201121
Qt.__qt_version__ = Qt._QtCore.QT_VERSION_STR
1122+
Qt.QtCompat.qInstallMessageHandler = _qInstallMessageHandler
11211123
Qt.QtCompat.translate = Qt._QtCore.QCoreApplication.translate
11221124

11231125
if hasattr(Qt, "_QtWidgets"):
@@ -1196,8 +1198,8 @@ def _pyqt4():
11961198
if hasattr(Qt, "_QtCore"):
11971199
Qt.__binding_version__ = Qt._QtCore.PYQT_VERSION_STR
11981200
Qt.__qt_version__ = Qt._QtCore.QT_VERSION_STR
1199-
12001201
QCoreApplication = Qt._QtCore.QCoreApplication
1202+
Qt.QtCompat.qInstallMessageHandler = _qInstallMessageHandler
12011203
Qt.QtCompat.translate = (
12021204
lambda context, sourceText, disambiguation, n:
12031205
QCoreApplication.translate(
@@ -1275,10 +1277,6 @@ def _loadUi(uifile, baseinstance=None):
12751277
return the newly created instance of the user interface.
12761278
12771279
"""
1278-
if hasattr(baseinstance, "layout") and baseinstance.layout():
1279-
message = ("QLayout: Attempting to add Layout to %s which "
1280-
"already has a layout")
1281-
raise RuntimeError(message % (baseinstance))
12821280

12831281
if hasattr(Qt, "_uic"):
12841282
return Qt._uic.loadUi(uifile, baseinstance)
@@ -1359,6 +1357,43 @@ def createWidget(self, class_name, parent=None, name=""):
13591357
raise NotImplementedError("No implementation available for loadUi")
13601358

13611359

1360+
def _qInstallMessageHandler(handler):
1361+
"""Install a message handler that works in all bindings
1362+
1363+
Args:
1364+
handler: A function that takes 3 arguments, or None
1365+
"""
1366+
def messageOutputHandler(*args):
1367+
# In Qt4 bindings, message handlers are passed 2 arguments
1368+
# In Qt5 bindings, message handlers are passed 3 arguments
1369+
# The first argument is a QtMsgType
1370+
# The last argument is the message to be printed
1371+
# The Middle argument (if passed) is a QMessageLogContext
1372+
if len(args) == 3:
1373+
msgType, logContext, msg = args
1374+
elif len(args) == 2:
1375+
msgType, msg = args
1376+
logContext = None
1377+
else:
1378+
raise TypeError(
1379+
"handler expected 2 or 3 arguments, got {0}".format(len(args)))
1380+
1381+
if isinstance(msg, bytes):
1382+
# In python 3, some bindings pass a bytestring, which cannot be
1383+
# used elsewhere. Decoding a python 2 or 3 bytestring object will
1384+
# consistently return a unicode object.
1385+
msg = msg.decode()
1386+
1387+
handler(msgType, logContext, msg)
1388+
1389+
passObject = messageOutputHandler if handler else handler
1390+
if Qt.IsPySide or Qt.IsPyQt4:
1391+
return Qt._QtCore.qInstallMsgHandler(passObject)
1392+
elif Qt.IsPySide2 or Qt.IsPyQt5:
1393+
return Qt._QtCore.qInstallMessageHandler(passObject)
1394+
1395+
1396+
13621397
def _convert(lines):
13631398
"""Convert compiled .ui file from PySide2 to Qt.py
13641399

0 commit comments

Comments
 (0)