[Development] Access lazy Models from within QML

Dominik Holland dominik.holland at pelagicore.com
Tue Mar 5 08:28:07 CET 2013


On 03/05/2013 01:51 AM, Alan Alpert wrote:
> whoops, missed reply-all
> On Mon, Mar 4, 2013 at 4:51 PM, Alan Alpert <416365416c at gmail.com> wrote:
>> On Mon, Mar 4, 2013 at 1:36 AM, Dominik Holland
>> <dominik.holland at pelagicore.com> wrote:
>>> Hi all,
>>> i'm currently in a project where i try to access a lazy C++ model from
>>> within QML.
>>> The basics are working well but when it comes to setting the
>>> currentIndex it becomes a bit odd.
>>> If i implemented everything well the lazy model always reports the
>>> element count it currently has loaded by rowCount(). If the List comes
>>> to the end, it will call canFetchMore() and if true also fetchMore() to
>>> get more elements.
>>> But when somebody tries to set the currentIndex to something which is
>>> currently not loaded into the lazy model, the data will not be loaded
>>> automatically and the Highlight set accordingly.
>>> I looked it up at the source code and the code of the QML Views are
>>> always checking the currentIndex to be smaller than the model's count.
>>> Because of this the View rejects the currentIndex instead of trying to
>>> load the full content of the model to determine whether the index is
>>> really out of bound.
>>> Some my question is, is this a Bug in QtDeclarative ? If so what do you
>>> think should be the right behavior ? Loading all the content just to
>>> check whether the index is in bound seems to me a bit too performance
>>> consuming.
>> This is not a bug, ListView does not currently support canFetchMore
>> (which only claims to be used by QAbstractItemView, so the docs are
>> accurate already). The behavior if it was supported seems simple: as
>> you scroll near the end the scrollbar jumps, and if you request an
>> index off the end it fetches more until it either reaches the index or
>> can't fetch more. You should file a suggestion in JIRA, long-term I
>> don't see why the QML views should not support this feature.
Ok will do that...
>> For now, this means that you'll need to manage fetching more data from
>> the model yourself. If you expose the functions to QML, it can be as
>> simple as (inside your ListView): onCurrentIndexChanged: if
>> (currentIndex > myModel.count * 0.9 && myModel.canFetchMore)
>> myModel.fetchMore();

Yes this makes sense and it's similar to my solution... But why do you 
need to multiply it with 0.9 ?


More information about the Development mailing list