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

Phil Thompson phil at riverbankcomputing.com
Mon Dec 8 09:51:11 GMT 2014


On 27/11/2014 1:36 am, B. B. wrote:
> 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/

Should be fixed in the current snapshot.

Phil


More information about the PyQt mailing list