[Development] [QML] ListModel discards item's method

Ben Lau xbenlau at gmail.com
Sat May 3 16:04:04 CEST 2014


Hi,

I have few questions about the implementation of ListModel and the design
principle about using models with object in QML application.

I would like to use a model class to hold a list of item (or  C++ QObject )
with custom methods / slot functions. However, I found that after the
item/QObject is appended to the ListModel, then access the model to get
back the object. The associated method will become a property value and not
able to call.

The following code demonstrates the problem:

import QtQuick 2.0

import QtTest 1.0


TestCase {


    Item {

        id : testItem

        property string name : "testObject"

        function testMethod() {

            return -1;

        }

    }


    ListModel {

        id : listModel

    }


    function test_ListModel() {

        listModel.append(testItem);

        compare(listModel.count , 1);


        var item = listModel.get(0);


        compare(item !== testItem, true); // They are not the same object

        compare(item.name , "testObject");


        expectFail("","The testMehod() should be a function but now it
becomes a property");

        compare(typeof item.testMethod , "function");

    }


}


I have checked the VisualItemModel / ObjectModel. They could hold
object but they are not based on QAbstractItemModel . They can not be
used as a dynamic data provider to another component like ListView.


So I am very confused with the design of
ListMode/VisualItemModel/VisualDataItemModel.


1) Why ListModel discard the method of its contained data?


2) What is the purpose of VisualItemModel/VisualDataItem? Why they are
not based on QAbstractItemModel?


3) Any alternative solution to store the object list?


Thank for you advise.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140503/def5fd03/attachment.html>


More information about the Development mailing list