[PySide] QUiLoader not loading layouts from .ui file

Steven Caron carons at gmail.com
Thu May 16 01:58:33 CEST 2013


Hello,

This is my first post to the list. Sorry for reposting my questions from
stackoverflow.com and qt-project forums but I am trying all avenues. My
original post on stackoverflow.com...

http://stackoverflow.com/questions/16492540/quiloader-not-loading-layouts-from-ui-file

I have a plugin for an application which provides the ability to use PyQt4
to create widgets which they can use to make their own tools for the
application. The QApplication is maintained by a C++ plugin and an 'anchor'
widget is parented to the host application's window handle. Then the user
can create their own widgets and use this 'anchor' to be the parent widget.
This works very well for PyQt4. Recently, I have been attempting to provide
support for PySide and this too is working well right up until I need to
create widgets from .ui files. It seems that when I use QUiLoader to load
my .ui file the my resulting widget doesn't look the same as in PyQt4. It
looks as if it is missing or skipping layouts and other properties
described in the .ui file, such as the title. Thing is when I log the
return value of that load function I get what seems right...

    class ExampleUiFile(QDialog):
        def __init__(self, parent, uifilepath):
            QDialog.__init__(self, parent)

            # load ui file
            loader = QUiLoader(self)
            file = QtCore.QFile(uifilepath)
            file.open(QtCore.QFile.ReadOnly)
            self.ui = loader.load(file, self)
            file.close()

            for k,v in vars(self.ui).items():
                print("%s : %s" % (k,v))

            # connect to the createCube function
            self.ui.uiCreateCube.clicked.connect(self.createCube)

        def createCube(self):
            print(self.ui.uiCubeName.text())

Output..

    horizontalLayout : <PySide.QtGui.QHBoxLayout object at
0x0000000023997308>
    uiCubeName : <PySide.QtGui.QLineEdit object at 0x0000000023338508>
    verticalLayout :<PySide.QtGui.QVBoxLayout object at 0x0000000023997548>
    gridLayout : <PySide.QtGui.QGridLayout object at 0x0000000023997E08>
    uiCubeLength : <PySide.QtGui.QDoubleSpinBox object at
0x0000000023338808>
    uiCreateCube : <PySide.QtGui.QPushButton object at 0x0000000023338988>

So in an attempt to fix this I went digging, here and elsewhere, and found
examples of sub-classing QUiLoader. I was able to copy a simple class which
inherits QUiLoader which does some extra work to return the initial base
class. This guy seemed to have worked and my dialog looks correct, the
correct title is shown, layout, and resizing works. Unfortunately, the
garbage collector removes all the widgets that my loader class created and
I get message about my object being deleted...

    class UiLoader(QUiLoader):
        def __init__(self, baseinstance):
            super(UiLoader, self).__init__(baseinstance)
            self._baseinstance = baseinstance

        def createWidget(self, classname, parent=None, name=""):
            widget = super(UiLoader, self).createWidget(
                classname, parent, name)

            if parent is None:
                return self._baseinstance
            else:
                setattr(self._baseinstance, name, widget)
                return widget

Output using new sub-classed loader...

    Internal C++ object (PySide.QtGui.QLineEdit) already deleted.

I have done a lot of digging on this issue and some bug in PySide in the
past seemed to be the culprit, but I am using PySide 1.1.2 which has it
fixed. I even built PySide from source with 1.1.3dev and still the same. I
should add that I wasn't able to reproduce this sub-classing issue outside
of the host application. I was able to make a simple python/PySide example
that worked as expected.

Where do I go from here, where should I look next? I get a good looking ui
without the functionality or I get an ugly ui with the functionality. I
would prefer not to sub-class the QUiLoader since I am not doing anything
fancy with custom widgets or anything.

Thanks,
Steven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130515/61c3ca63/attachment.html>


More information about the PySide mailing list