Skip to content

Instantly share code, notes, and snippets.

@altendky
Last active December 28, 2018 16:27
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 altendky/b49af568393c2a4c597cdfbcbdfd5bdb to your computer and use it in GitHub Desktop.
Save altendky/b49af568393c2a4c597cdfbcbdfd5bdb to your computer and use it in GitHub Desktop.

When loading the bad.ui which has a QStackedWidget holding the QTabWidget. Subsequent loads in the same process seem ok. The issue directly observed in my original code was that .findChildren() was unable to find the nvview.NvView widget. Here we can see that the QTabWidget isn't getting it's parent set to the QStackedWidget. I am assuming the QStackedWidget with the QTabWidget as the parent (in both bad and good cases) is an expected internal piece of the QTabWidget implementation.

The good.ui does not have the QStackedWidget and seems to load fine regardless.

I tested this for each version on pypi back to 5.8 and they all seem to act the same. I have had code 'like' this (I deleted 99% or whatever to get down to this sscce) for a long time and it's been fine. I'm not sure why I started running into this only recently when I tried to add another tab.

bad.ui ++++
---- bad.ui - 0
found []
stacked <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf863c18>
parentage [<nvview.NvView object at 0x7fc4bf82d5e8>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf86e708>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf845dc8>, <PyQt5.QtWidgets.QTabWidget object at 0x7fc4bf8668b8>]
---- bad.ui - 1
found [<nvview.NvView object at 0x7fc4bf807288>]
stacked <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf807048>
parentage [<nvview.NvView object at 0x7fc4bf807288>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf807168>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf86e708>, <PyQt5.QtWidgets.QTabWidget object at 0x7fc4bf8070d8>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf807048>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf845f78>]
---- bad.ui - 2
found [<nvview.NvView object at 0x7fc4bf841438>]
stacked <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf82d708>
parentage [<nvview.NvView object at 0x7fc4bf841438>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf845e58>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf86e708>, <PyQt5.QtWidgets.QTabWidget object at 0x7fc4bf845dc8>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf82d708>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf8668b8>]
good.ui ++++
---- good.ui - 0
found [<nvview.NvView object at 0x7fc4bf8071f8>]
stacked None
parentage [<nvview.NvView object at 0x7fc4bf8071f8>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf8070d8>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf86e708>, <PyQt5.QtWidgets.QTabWidget object at 0x7fc4bf807048>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf845ee8>]
---- good.ui - 1
found [<nvview.NvView object at 0x7fc4bf841288>]
stacked None
parentage [<nvview.NvView object at 0x7fc4bf841288>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf863c18>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf86e708>, <PyQt5.QtWidgets.QTabWidget object at 0x7fc4bf8635e8>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf8668b8>]
---- good.ui - 2
found [<nvview.NvView object at 0x7fc4bf807168>]
stacked None
parentage [<nvview.NvView object at 0x7fc4bf807168>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf807048>, <PyQt5.QtWidgets.QStackedWidget object at 0x7fc4bf86e708>, <PyQt5.QtWidgets.QTabWidget object at 0x7fc4bf82d5e8>, <PyQt5.QtWidgets.QWidget object at 0x7fc4bf845f78>]
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>824</width>
<height>809</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QStackedWidget" name="breaking_stack">
<widget class="QTabWidget" name="tabs">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="other">
<attribute name="title">
<string>Parameters</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="NvView" name="nv" native="true"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="files">
<attribute name="title">
<string>Files</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="FilesView" name="files_view" native="true"/>
</item>
</layout>
</widget>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>NvView</class>
<extends>QWidget</extends>
<header>nvview</header>
</customwidget>
<customwidget>
<class>FilesView</class>
<extends>QWidget</extends>
<header>filesview</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
import pathlib
import PyQt5.uic
Ui, UiBase = PyQt5.uic.loadUiType(
pathlib.Path(__file__).with_suffix('.ui'),
)
class FilesView(UiBase):
def __init__(self, parent):
super().__init__(parent=parent)
self.ui = Ui()
self.ui.setupUi(self)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="0">
<widget class="QPushButton" name="blue"/>
</item>
</layout>
</widget>
<customwidgets/>
<resources/>
<connections/>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>824</width>
<height>809</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabs">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="other">
<attribute name="title">
<string>Parameters</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="NvView" name="nv" native="true"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="files">
<attribute name="title">
<string>Files</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="FilesView" name="files_view" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>NvView</class>
<extends>QWidget</extends>
<header>nvview</header>
</customwidget>
<customwidget>
<class>FilesView</class>
<extends>QWidget</extends>
<header>filesview</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
import pathlib
from PyQt5 import uic
# See file COPYING in this source tree
__copyright__ = 'Copyright 2018, EPC Power Corp.'
__license__ = 'GPLv2+'
Ui, UiBase = uic.loadUiType(pathlib.Path(__file__).with_suffix('.ui'))
class NvView(UiBase):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.ui = Ui()
self.ui.setupUi(self)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="windowTitle">
<string>Form</string>
</property>
</widget>
<customwidgets/>
<resources/>
<connections/>
</ui>
PyQt5==5.11.3
PyQt5-sip==4.19.13
from PyQt5 import QtWidgets, uic
import nvview
app = QtWidgets.QApplication([])
for ui_name in ('bad.ui', 'good.ui'):
print('\n\n\n {} ++++'.format(ui_name))
for i in range(3):
print('\n ---- {} - {}'.format(ui_name, i))
ui = uic.loadUi(ui_name)
print(' found', ui.findChildren(nvview.NvView))
print(' stacked', getattr(ui, 'breaking_stack', None))
parents = []
parent = ui.nv
while parent is not None:
parents.append(parent)
parent = parent.parent()
print('parentage', parents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment