1 | /*!
|
---|
2 | \example itemviews/fetchmore
|
---|
3 | \title Fetch More Example
|
---|
4 |
|
---|
5 | The Fetch More example shows how two add items to an item view
|
---|
6 | model on demand.
|
---|
7 |
|
---|
8 | \image fetchmore-example.png
|
---|
9 |
|
---|
10 | The user of the example can enter a directory in the \gui
|
---|
11 | Directory line edit. The contents of the directory will
|
---|
12 | be listed in the list view below.
|
---|
13 |
|
---|
14 | When you have large - or perhaps even infinite - data sets, you
|
---|
15 | will need to add items to the model in batches, and preferably only
|
---|
16 | when the items are needed by the view (i.e., when they are visible
|
---|
17 | in the view).
|
---|
18 |
|
---|
19 | In this example, we implement \c FileListModel - an item view
|
---|
20 | model containing the entries of a directory. We also have \c
|
---|
21 | Window, which sets up the GUI and feeds the model with
|
---|
22 | directories.
|
---|
23 |
|
---|
24 | Let's take a tour of \c {FileListModel}'s code.
|
---|
25 |
|
---|
26 | \section1 FileListModel Class Definition
|
---|
27 |
|
---|
28 | The \c FileListModel inherits QAbstractListModel and contains the
|
---|
29 | contents of a directory. It will add items to itself only when
|
---|
30 | requested to do so by the view.
|
---|
31 |
|
---|
32 | \snippet examples/itemviews/fetchmore/filelistmodel.h 0
|
---|
33 |
|
---|
34 | The secret lies in the reimplementation of
|
---|
35 | \l{QAbstractItemModel::}{fetchMore()} and
|
---|
36 | \l{QAbstractItemModel::}{canFetchMore()} from QAbstractItemModel.
|
---|
37 | These functions are called by the item view when it needs more
|
---|
38 | items.
|
---|
39 |
|
---|
40 | The \c setDirPath() function sets the directory the model will
|
---|
41 | work on. We emit \c numberPopulated() each time we add a batch of
|
---|
42 | items to the model.
|
---|
43 |
|
---|
44 | We keep all directory entries in \c fileList. \c fileCount is the
|
---|
45 | number of items that have been added to the model.
|
---|
46 |
|
---|
47 | \section1 FileListModel Class Implementation
|
---|
48 |
|
---|
49 | We start by checking out the \c setDirPath().
|
---|
50 |
|
---|
51 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 0
|
---|
52 |
|
---|
53 | We use a QDir to get the contents of the directory. We need to
|
---|
54 | inform QAbstractItemModel that we want to remove all items - if
|
---|
55 | any - from the model.
|
---|
56 |
|
---|
57 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 1
|
---|
58 |
|
---|
59 | The \c canFetchMore() function is called by the view when it needs
|
---|
60 | more items. We return true if there still are entries that we have
|
---|
61 | not added to the model; otherwise, we return false.
|
---|
62 |
|
---|
63 | And now, the \c fetchMore() function itself:
|
---|
64 |
|
---|
65 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 2
|
---|
66 |
|
---|
67 | We first calculate the number of items to fetch.
|
---|
68 | \l{QAbstractItemModel::}{beginInsertRows()} and
|
---|
69 | \l{QAbstractItemModel::}{endInsertRows()} are mandatory for
|
---|
70 | QAbstractItemModel to keep up with the row insertions. Finally, we
|
---|
71 | emit \c numberPopulated(), which is picked up by \c Window.
|
---|
72 |
|
---|
73 | To complete the tour, we also look at \c rowCount() and \c data().
|
---|
74 |
|
---|
75 | \snippet examples/itemviews/fetchmore/filelistmodel.cpp 4
|
---|
76 |
|
---|
77 | Notice that the row count is only the items we have added so far,
|
---|
78 | i.e., not the number of entries in the directory.
|
---|
79 |
|
---|
80 | In \c data(), we return the appropriate entry from the \c
|
---|
81 | fileList. We also separate the batches with a different background
|
---|
82 | color.
|
---|
83 | */
|
---|
84 |
|
---|