Skip to content

Instantly share code, notes, and snippets.

@rutsky
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rutsky/b44d7a9abe4fe0c89dc0 to your computer and use it in GitHub Desktop.
Save rutsky/b44d7a9abe4fe0c89dc0 to your computer and use it in GitHub Desktop.
PyQt bug: UI files loading/parsing is not reentrant (PyQt 5.4.2 nightly d5b00e76067f)
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QWidget, QApplication
# Problem: the ui loader/parser is not reentrant. If another UI load is requested
# during loading of UI file, final layout may be corrupted.
#
# In this example "main.ui" file requests promoted widget from "widget.py", and
# on module level of "widget.py" UI file "widget.ui" is being loaded.
# A workaround is to force loading of all UI files independently, e.g. by loading
# "widget.ui" first by importing "widget.py" module below.
#import widget
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self._ui = uic.loadUi("main.ui", self)
def main():
app = QApplication(sys.argv)
w = MainWidget()
w.resize(200, 100)
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>870</width>
<height>578</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string notr="true">Form</string>
</property>
<property name="childrenCollapsible" stdset="0">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>1. Main label</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>2. Main button, Widget should be below this button</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="MyWidget" name="widget" native="true"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>MyWidget</class>
<extends>QWidget</extends>
<header>widget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
from PyQt5 import uic
_Ui_Widget, _WidgetBase = uic.loadUiType("widget.ui")
class MyWidget(_WidgetBase):
def __init__(self, parent=None):
super().__init__(parent)
self._ui = _Ui_Widget()
self._ui.setupUi(self)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MyWidget</class>
<widget class="QWidget" name="MyWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>442</width>
<height>286</height>
</rect>
</property>
<property name="windowTitle">
<string>Unrecognized</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,0">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>3. Widget button</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>4. Widget label</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>5. Widget button</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment