1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example itemviews/fetchmore
30 \title Fetch More Example
32 \brief The Fetch More example shows how two add items to an item view
35 \image fetchmore-example.png
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.
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
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
51 Let's take a tour of \c {FileListModel}'s code.
53 \section1 FileListModel Class Definition
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.
59 \snippet examples/itemviews/fetchmore/filelistmodel.h 0
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
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
71 We keep all directory entries in \c fileList. \c fileCount is the
72 number of items that have been added to the model.
74 \section1 FileListModel Class Implementation
76 We start by checking out the \c setDirPath().
78 \snippet examples/itemviews/fetchmore/filelistmodel.cpp 0
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
84 \snippet examples/itemviews/fetchmore/filelistmodel.cpp 1
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.
90 And now, the \c fetchMore() function itself:
92 \snippet examples/itemviews/fetchmore/filelistmodel.cpp 2
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.
100 To complete the tour, we also look at \c rowCount() and \c data().
102 \snippet examples/itemviews/fetchmore/filelistmodel.cpp 4
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.
107 In \c data(), we return the appropriate entry from the \c
108 fileList. We also separate the batches with a different background