#5 Crash when using PythonQt library with temporary objects

open-wont-fix
nobody
None
5
2011-02-05
2011-02-03
Chris Bevan
No

The following code causes a crash:

from PythonQt import QtGui as gui
widget = gui.QWidget()
layout = gui.QVBoxLayout()
layout.addWidget(gui.QLabel("Hello"))
widget.setLayout(layout)
widget.show()

The following code doesn't:

from PythonQt import QtGui as gui
widget = gui.QWidget()
layout = gui.QVBoxLayout()
widget.setLayout(layout)
layout.addWidget(gui.QLabel("Hello"))
widget.show()

The reason for this seems to be that the line "layout.addWidget(gui.QLabel('Hello'))" creates a temporary label widget and adds it to the layout, but a reference to it is only stored if the layout is already assigned to another widget - presumably by setting the temporary widget's parent to the layout's widget.

This happens in the PyScriptingConsole example as well as my application, though PyScriptingConsole comes up with a crash when typing the last line as the completer tries to inspect the properties of the widget.

I can work around this by using the second form, but getting users of my application to do so is a bit more complicated :)

Discussion

  • Chris Bevan
    Chris Bevan
    2011-02-03

    Another workaround is to store the widget, of course:

    label = gui.QLabel("Hello")
    layout.addWidget(label)

    ... but again, the usage with the crash can trip people up (like me).

     
  • Florian Link
    Florian Link
    2011-02-05

    The problem is indeed that the temporary object has no parent (seems like QLayout only does setParent() on it's children when the layout gets a parent. Another workaround is to give the label a parent in its constructor.

    Since PythonQt handles ownership via QObjects parent() and your Label has parent()==NULL, PythonQt frees it.
    The only solution that could fix that would be explicit ownership handling (which e.g. PySide does), but that requires some mayor extensions to the code generator.

    I see no easy way to fix this, since I will not have time to add explicit ownership handling to PythonQt.

     
  • Florian Link
    Florian Link
    2011-02-05

    • status: open --> open-wont-fix
     
  • Chris Bevan
    Chris Bevan
    2011-02-07

    Hi Florian - thanks for the response. I've encountered some other cases where this can be a problem, such as creating QTreeWidgets and QTreeWidgetItems; adding the items to the widget then causes problems with clearing the widget, because Qt then deletes the items, but PythonQt will also try to when its objects go out of scope. In that case I just had to not clear the widget, because deleting PythonQt's objects worked fine. In general I think these issues all have workarounds, but I'm mainly trying to find a way to stop it crashing :)

    I can imagine that the ownership issue would be pretty hard to fix. I'll be taking a look into it further when I get a chance and see if there's anything simple I can do to PythonQt locally to solve the problem. Thanks anyway.