[Qt-qml] Accessing a QAbstractItemModel in QML outside a view
Jan Ekholm
jan.ekholm at smultron.net
Wed Dec 1 08:59:57 CET 2010
Martin, thanks for the hint!
Ok, I guess that would work. Not exactly straightforward though. I assume it will also work when
I need to iterate over all items in a model to do some operation on all of them, but by indexing
using something.
Hm, I looked at the API and it seems I can't access items randomly from outside the view
either. So the best way is to make a proper API in C++. Not ideal, but takes a lot less time than
trying to do it in QML...
On Dec 1, 2010, at 9:26 , <martin.jones at nokia.com> <martin.jones at nokia.com> wrote:
> Right now if you want to access data in your model in script, you will need to add methods to your model to allow that.
>
> For the specific case of showing data from the currentItem, you can expose the model data, e.g.
>
> ListView {
> Id: list
> delegate: Text {
> property variant data: model
> text: name
> }
> }
> Text { text: list.currentItem.data.name }
>
> BR,
> Martin.
>
>> -----Original Message-----
>> From: qt-qml-bounces at trolltech.com [mailto:qt-qml-bounces at trolltech.com] On
>> Behalf Of ext Jan Ekholm
>> Sent: Wednesday, 1 December 2010 4:55 PM
>> To: qt-qml at trolltech.com
>> Subject: [Qt-qml] Accessing a QAbstractItemModel in QML outside a view
>>
>>
>> Hi,
>>
>> The subject says it all, apart from the context. Accessing a QAIM in a view (such
>> as
>> a ListView) is all nicely documented and works as expected. However, how can I
>> access
>> the same exposed C++ model outside a view? In my case I want to show a list of
>> data and
>> next to the list have some Text/Image items with more information about the
>> selected item
>> in the view. Totally normal stuff.
>>
>> Getting the index of the selected row is easy, the view gives me that. Getting the
>> model
>> instance is also easy, it's in the root context. However, getting data out of the
>> model seems
>> to be impossible. I've tried all kinds of ways to access the data but none works.
>> However, if
>> I look here:
>>
>> http://doc.trolltech.com/4.7/qdeclarativemodels.html#accessing-views-and-models-
>> from-delegates
>>
>> it's seems that delegates can even write data to a model. This gives a somewhat
>> perverse
>> situation where you can inside delegates read and write to models but outside a
>> delegate
>> you can't even read data.
>>
>> So, this can't possibly be the case and there's something my thick brain just does
>> not grok?
>> I mean, models are one of the few ways you can get real data into a QML
>> document, and not
>> being able to actually access it makes it all feel somewhat silly. I can make extra
>> methods
>> on my subclassed QAIM on the C++ side to get data, but the point is that I should
>> not have
>> to do that, it ought to just work.
>>
>> Someone please give me some RTFM resource.
>>
>> Best regards,
>> Jan Ekholm
>>
>> --
>> Jan Ekholm
>> jan.ekholm at smultron.net
>>
>>
>>
>>
>> _______________________________________________
>> Qt-qml mailing list
>> Qt-qml at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-qml
--
Jan Ekholm
jan.ekholm at smultron.net
More information about the Qt-qml
mailing list