source: trunk/doc/src/examples/simpletreemodel.qdoc@ 677

Last change on this file since 677 was 651, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 15.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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:LGPL$
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
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \example itemviews/simpletreemodel
44 \title Simple Tree Model Example
45
46 The Simple Tree Model example shows how to create a basic, read-only
47 hierarchical model to use with Qt's standard view classes. For a
48 description of simple non-hierarchical list and table models, see the
49 \l{model-view-programming.html}{Model/View Programming} overview.
50
51 \image simpletreemodel-example.png
52
53 Qt's model/view architecture provides a standard way for views to manipulate
54 information in a data source, using an abstract model of the data to
55 simplify and standardize the way it is accessed. Simple models represent
56 data as a table of items, and allow views to access this data via an
57 \l{model-view-model.html}{index-based} system. More generally, models can
58 be used to represent data in the form of a tree structure by allowing each
59 item to act as a parent to a table of child items.
60
61 Before attempting to implement a tree model, it is worth considering whether
62 the data is supplied by an external source, or whether it is going to be
63 maintained within the model itself. In this example, we will implement an
64 internal structure to hold data rather than discuss how to package data from
65 an external source.
66
67 \section1 Design and Concepts
68
69 The data structure that we use to represent the structure of the data takes
70 the form of a tree built from \c TreeItem objects. Each \c TreeItem
71 represents an item in a tree view, and contains several columns of data.
72
73 \target SimpleTreeModelStructure
74 \table
75 \row \i \inlineimage treemodel-structure.png
76 \i \bold{Simple Tree Model Structure}
77
78 The data is stored internally in the model using \c TreeItem objects that
79 are linked together in a pointer-based tree structure. Generally, each
80 \c TreeItem has a parent item, and can have a number of child items.
81 However, the root item in the tree structure has no parent item and it
82 is never referenced outside the model.
83
84 Each \c TreeItem contains information about its place in the tree
85 structure; it can return its parent item and its row number. Having
86 this information readily available makes implementing the model easier.
87
88 Since each item in a tree view usually contains several columns of data
89 (a title and a summary in this example), it is natural to store this
90 information in each item. For simplicity, we will use a list of QVariant
91 objects to store the data for each column in the item.
92 \endtable
93
94 The use of a pointer-based tree structure means that, when passing a
95 model index to a view, we can record the address of the corresponding
96 item in the index (see QAbstractItemModel::createIndex()) and retrieve
97 it later with QModelIndex::internalPointer(). This makes writing the