[PyQt] Qml: Instantiate a model derived from QAbstractListModel from qml.

B. B. thebbzoo at gmail.com
Thu Nov 27 01:36:15 GMT 2014


Hey,

I get a segmentation fault, when I use an instance of a ListModel in qml.
The model is implemented in python, registered with the qml type system.

In short, adapting the example from the PyQt sources:
"examples/quick/models/abstractitemsmodel".

------------------------------------------------------------
The example creates the model "outside qml",

 model = AnimalModel()
 model.addAnimal(Animal("Wolf", "Medium"))
.
and register the object with the rootContext

 ctxt = view.rootContext()
 ctxt.setContextProperty('myModel', model)

Using it in qml like this:

ListView {
    width: 200; height: 250

    model: myModel
    delegate: Text { text: "Animal: " + type + ", " + size }
}

----------------------------------------------------------------------
When I register the model as a qml type :

 qmlRegisterType( AnimalModel, "AnimalModel", 1, 0, "AnimalModel" )

and instantiate in qml like this.

import AnimalModel 1.0
ListView {
    width: 200; height: 250

    model: AnimalModel{ id: animalModel }

    delegate: Text { text: "Animal: " + type + ", " + size }
}

I get a segmentation fault.
------------------------------------------------------------------------

I admit I do still not have the big golden overview over PyQt5 and Qml,
but I think / hope :-) this should be perfectly legal....

I have attached the adapted example. In two versions. One using the pyrcc
for the qml file - exactly like the example :

abstractitemmodel.py
abstractitemmodel.qrc
abstractitemmodel_rc.py
view.qml

and one just refer directly to the qml file...

dmodel.py
dview.qml


I am using a fairly new daily snapshot :
PyQt-gpl-5.4-snapshot-15b37c22d541/


Best Regards


Brian.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: abstractitemmodel.py
Type: text/x-python
Size: 3929 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0003.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: abstractitemmodel.qrc
Type: application/octet-stream
Size: 93 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0003.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: abstractitemmodel_rc.py
Type: text/x-python
Size: 9866 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0004.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: view.qml
Type: application/octet-stream
Size: 2199 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0004.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dmodel.py
Type: text/x-python
Size: 4241 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0005.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dview.qml
Type: application/octet-stream
Size: 2198 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141126/2ef95f2b/attachment-0005.obj>


More information about the PyQt mailing list