1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 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/stardelegate
|
---|
44 | \title Star Delegate Example
|
---|
45 |
|
---|
46 | The Star Delegate example shows how to create a delegate that
|
---|
47 | can paint itself and that supports editing.
|
---|
48 |
|
---|
49 | \image stardelegate.png The Star Delegate Example
|
---|
50 |
|
---|
51 | When displaying data in a QListView, QTableView, or QTreeView,
|
---|
52 | the individual items are drawn by a
|
---|
53 | \l{Delegate Classes}{delegate}. Also, when the user starts
|
---|
54 | editing an item (e.g., by double-clicking the item), the delegate
|
---|
55 | provides an editor widget that is placed on top of the item while
|
---|
56 | editing takes place.
|
---|
57 |
|
---|
58 | Delegates are subclasses of QAbstractItemDelegate. Qt provides
|
---|
59 | QItemDelegate, which inherits QAbstractItemDelegate and handles
|
---|
60 | the most common data types (notably \c int and QString). If we
|
---|
61 | need to support custom data types, or want to customize the
|
---|
62 | rendering or the editing for existing data types, we can subclass
|
---|
63 | QAbstractItemDelegate or QItemDelegate. See \l{Delegate Classes}
|
---|
64 | for more information about delegates, and \l{Model/View
|
---|
65 | Programming} if you need a high-level introduction to Qt's
|
---|
66 | model/view architecture (including delegates).
|
---|
67 |
|
---|
68 | In this example, we will see how to implement a custom delegate
|
---|
69 | to render and edit a "star rating" data type, which can store
|
---|
70 | values such as "1 out of 5 stars".
|
---|
71 |
|
---|
72 | The example consists of the following classes:
|
---|
73 |
|
---|
74 | \list
|
---|
75 | \o \c StarRating is the custom data type. It stores a rating
|
---|
76 | expressed as stars, such as "2 out of 5 stars" or "5 out of
|
---|
77 | 6 stars".
|
---|
78 |
|
---|
79 | \o \c StarDelegate inherits QItemDelegate and provides support
|
---|
80 | for \c StarRating (in addition to the data types already
|
---|
81 | handled by QItemDelegate).
|
---|
82 |
|
---|
83 | \o \c StarEditor inherits QWidget and is used by \c StarDelegate
|
---|
84 | to let the user edit a star rating using the mouse.
|
---|
85 | \endlist
|
---|
86 |
|
---|
87 | To show the \c StarDelegate in action, we will fill a
|
---|
88 | QTableWidget with some data and install the delegate on it.
|
---|
89 |
|
---|
90 | \section1 StarDelegate Class Definition
|
---|
91 |
|
---|
92 | Here's the definition of the \c StarDelegate class:
|
---|
93 |
|
---|
94 | \snippet examples/itemviews/stardelegate/stardelegate.h 0
|
---|
95 |
|
---|
96 | All public functions are reimplemented virtual functions from
|
---|
97 | QItemDelegate to provide custom rendering and editing.
|
---|
98 |
|
---|
99 | \section1 StarDelegate Class Implementation
|
---|
100 |
|
---|
101 | The \l{QAbstractItemDelegate::}{paint()} function is
|
---|
102 | reimplemented from QItemDelegate and is called whenever the view
|
---|
103 | needs to repaint an item:
|
---|
104 |
|
---|
105 | \snippet examples/itemviews/stardelegate/stardelegate.cpp 0
|
---|
106 |
|
---|
107 | The function is invoked once for each item, represented by a
|
---|
108 | QModelIndex object from the model. If the data stored in the item
|
---|
109 | is a \c StarRating, we paint it ourselves; otherwise, we let
|
---|
110 | QItemDelegate paint it for us. This ensures that the \c
|
---|
111 | StarDelegate can handle the most common data types.
|
---|
112 |
|
---|
113 | In the case where the item is a \c StarRating, we draw the
|
---|
114 | background if the item is selected, and we draw the item using \c
|
---|
115 | StarRating::paint(), which we will review later.
|
---|
116 |
|
---|
117 | \c{StartRating}s can be stored in a QVariant thanks to the
|
---|
118 | Q_DECLARE_METATYPE() macro appearing in \c starrating.h. More on
|
---|
119 | this later.
|
---|
120 |
|
---|
121 | The \l{QAbstractItemDelegate::}{createEditor()} function is
|
---|
122 | called when the user starts editing an item:
|
---|
123 |
|
---|
124 | \snippet examples/itemviews/stardelegate/stardelegate.cpp 2
|
---|
125 |
|
---|
126 | If the item is a \c StarRating, we create a \c StarEditor and
|
---|
127 | connect its \c editingFinished() signal to our \c
|
---|
128 | commitAndCloseEditor() slot, so we can update the model when the
|
---|
129 | editor closes.
|
---|
130 |
|
---|
131 | Here's the implementation of \c commitAndCloseEditor():
|
---|
132 |
|
---|
133 | \snippet examples/itemviews/stardelegate/stardelegate.cpp 5
|
---|
134 |
|
---|
135 | When the user is done editing, we emit
|
---|
136 | \l{QAbstractItemDelegate::}{commitData()} and
|
---|
137 | \l{QAbstractItemDelegate::}{closeEditor()} (both declared in
|
---|
138 | QAbstractItemDelegate), to tell the model that there is edited
|
---|
139 | data and to inform the view that the editor is no longer needed.
|
---|
140 |
|
---|
141 | The \l{QAbstractItemDelegate::}{setEditorData()} function is
|
---|
142 | called when an editor is created to initialize it with data
|
---|
143 | from the model:
|
---|
144 |
|
---|
145 | \snippet examples/itemviews/stardelegate/stardelegate.cpp 3
|
---|
146 |
|
---|
147 | We simply call \c setStarRating() on the editor.
|
---|
148 |
|
---|
149 | The \l{QAbstractItemDelegate::}{setModelData()} function is
|
---|
150 | called when editing is finished, to commit data from the editor
|
---|
151 | to the model:
|
---|
152 |
|
---|
153 | \snippet examples/itemviews/stardelegate/stardelegate.cpp 4
|
---|
154 |
|
---|
155 | The \c sizeHint() function returns an item's preferred size:
|
---|
156 |
|
---|
157 | \snippet examples/itemviews/stardelegate/stardelegate.cpp 1
|
---|
158 |
|
---|
159 | We simply forward the call to \c StarRating.
|
---|
160 |
|
---|
161 | \section1 StarEditor Class Definition
|
---|
162 |
|
---|
163 | The \c StarEditor class was used when implementing \c
|
---|
164 | StarDelegate. Here's the class definition:
|
---|
|
---|