[PyQt] Trouble with parenting in loaded .ui... but only on first load?

Phil Thompson phil at riverbankcomputing.com
Fri Dec 28 22:23:56 GMT 2018


On 28 Dec 2018, at 7:15 pm, Kyle Altendorf <sda at fstab.net> wrote:
> 
> On 2018-12-28 11:50, Kyle Altendorf wrote:
>> https://gist.github.com/altendky/b49af568393c2a4c597cdfbcbdfd5bdb
>> 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.
> 
> https://gist.github.com/altendky/983344eb45776b94395cf460713b92ce
> 
> I got some help in #pyqt digging into this.  I added some debugging to uiparser.py and noticed that on the first load for bad, there were two reports of `UI version is 4.0` in the logging.  bad.ui uses the custom widget which loads fileview.ui so that makes sense.  But once uic comes out of the inner load, the outer load starts messing up.  It seems that the types have been changed around and so the `isinstance(item, QtWidgets.QWidget)` in `WidgetStack.popWidget()` starts failing.  You can look at the identifiers in the log but even the repr's have been totally changed.  So despite there  certainly being things that uic wants to match in that loop, they don't match.
> 
> One work around is to manually import the fileview module ahead of time so it's already parsed.  This makes sense with why it works after subsequent loads of the .ui as well.

It wouldn't surprise me at all if nested loads didn't work.

Phil


More information about the PyQt mailing list