1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example itemviews/fetchmore
|
---|
30 | \title Fetch More Example
|
---|
31 |
|
---|
32 | The Fetch More example shows how two add items to an item view
|
---|
33 | model on demand.
|
---|
34 |
|
---|
35 | \image fetchmore-example.png
|
---|
36 |
|
---|
37 | The user of the example can enter a directory in the \gui
|
---|
38 | Directory line edit. The contents of the directory will
|
---|
39 | be listed in the list view below.
|
---|
40 |
|
---|
41 | When you have large - or perhaps even infinite - data sets, you
|
---|
42 | will need to add items to the model in batches, and preferably only
|
---|
43 | when the items are needed by the view (i.e., when they are visible
|
---|
44 | in the view).
|
---|
45 |
|
---|
46 | In this example, we implement \c FileListModel - an item view
|
---|
47 | model containing the entries of a directory. We also have \c
|
---|
48 | Window, which sets up the GUI and feeds the model with
|
---|
49 | directories.
|
---|
50 |
|
---|
51 | Let's take a tour of \c {FileListModel}'s code.
|
---|
52 |
|
---|
53 | \section1 FileListModel Class Definition
|
---|
54 |
|
---|
55 | The \c FileListModel inherits QAbstractListModel and contains the
|
---|
56 | contents of a directory. It will add items to itself only when
|
---|
57 | requested to do so by the view.
|
---|
58 |
|
---|
59 | \snippet examples/itemviews/fetchmore/filelistmodel.h 0
|
---|
60 |
|
---|
61 | The secret lies in the reimplementation of
|
---|
62 | \l{QAbstractItemModel::}{fetchMore()} and
|
---|
63 | \l{QAbstractItemModel::}{canFetchMore()} from QAbstractItemModel.
|
---|
64 | These functions are called by the item view when it needs more
|
---|
65 | items.
|
---|
66 |
|
---|
67 | The \c setDirPath() function sets the directory the model will
|
---|
68 | work on. We emit \c numberPopulated() each time we add a batch of
|
---|
69 | items to the model.
|
---|
70 |
|
---|
71 | We keep all directory entries in \c fileList. \c fileCount is the
|
---|
72 | number of items that have been added to the model.
|
---|
73 |
|
---|
74 | \section1 FileListModel Class Implementation
|
---|
75 |
|
---|
76 | We start by checking out the \c setDirPath().
|
---|
77 |
|
---|
78 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 0
|
---|
79 |
|
---|
80 | We use a QDir to get the contents of the directory. We need to
|
---|
81 | inform QAbstractItemModel that we want to remove all items - if
|
---|
82 | any - from the model.
|
---|
83 |
|
---|
84 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 1
|
---|
85 |
|
---|
86 | The \c canFetchMore() function is called by the view when it needs
|
---|
87 | more items. We return true if there still are entries that we have
|
---|
88 | not added to the model; otherwise, we return false.
|
---|
89 |
|
---|
90 | And now, the \c fetchMore() function itself:
|
---|
91 |
|
---|
92 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 2
|
---|
93 |
|
---|
94 | We first calculate the number of items to fetch.
|
---|
95 | \l{QAbstractItemModel::}{beginInsertRows()} and
|
---|
96 | \l{QAbstractItemModel::}{endInsertRows()} are mandatory for
|
---|
97 | QAbstractItemModel to keep up with the row insertions. Finally, we
|
---|
98 | emit \c numberPopulated(), which is picked up by \c Window.
|
---|
99 |
|
---|
100 | To complete the tour, we also look at \c rowCount() and \c data().
|
---|
101 |
|
---|
102 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 4
|
---|
103 |
|
---|
104 | Notice that the row count is only the items we have added so far,
|
---|
105 | i.e., not the number of entries in the directory.
|
---|
106 |
|
---|
107 | In \c data(), we return the appropriate entry from the \c
|
---|
108 | fileList. We also separate the batches with a different background
|
---|
109 | color.
|
---|
110 | */
|
---|
111 |
|
---|