[PyQt] segfault!...and more
Aaron Digulla
digulla at hepe.com
Fri Jan 18 19:32:59 GMT 2008
Alberto Berti schrieb:
> Hi Aaron,
>
>>>>>> "Aaron" == Aaron Digulla <digulla at hepe.com> writes:
> >> # qui parent è in delegato, perciò parent.parent() è la #
> >> tabella view = parent.parent() index = view.currentIndex() item
> >> = index.internalPointer() if isinstance(item, LookupItem):
> >> widget = QComboBox(parent)
> >> widget.setModel(item.column.lookup_model)
>
> Aaron> Try
>
> Aaron> widget._modelRef = item.column.lookup_model
> Aaron> widget.setModel(item.column.lookup_model)
>
> Aaron> instead of
>
> Aaron> widget.setModel(item.column.lookup_model)
>
> Aaron> Does that help?
>
> Thanks for the hint, but it does not work the segfault is still
> raised. Maybe it's a combination of more issues. But just to know,
> what's the meaning of that ._modelRef setting?
It means nothing as such; you can use any name. The background is that
you have two memory manages: Qt and Python and both think they "own" the
objects. The problems start when they disagree and one side destroys an
object while the other side is still using it.
Models are such a case. When you create a model with Model() and just
set it in some widget, the widget will expect that someone else manages
the lifetime of the model. Python will think that no one uses the model
anymore as soon as the variable which contains the last pointer to it
goes out of scope:
def demo(textEdit):
model = QTextDocument()
textEdit.setModel(model)
As demo() ends, so does the lifetime of "model" and the object stored
behind it gets destroyed much to the annoyance of textEdit. So I'm
always saving a pointer of all models in the object in which I set them;
this makes sure that a model can never be deleted accidentally by
Python. Also, since I'm always doing this, I can't have any unused
models left. So my code looks:
def demo(textEdit):
model = QTextDocument()
textEdit._modelRef = model
textEdit.setModel(model)
That plugs one hole and can't hurt. I'm using "_modelRef" because it
tells what it is: An internal variable ("_") and a reference to the model.
Regards,
--
Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."
http://darkviews.blogspot.com/ http://www.pdark.de/
More information about the PyQt
mailing list