b36176b04260ab12d3f92c4eb479e4623f0d31ff
[profile/ivi/qtbase.git] / doc / src / examples / spinboxdelegate.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
13 ** this file.
14 **
15 ** Other Usage
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
18 ** and Nokia.
19 **
20 **
21 **
22 **
23 **
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29     \example itemviews/spinboxdelegate
30     \title Spin Box Delegate Example
31
32     The Spin Box Delegate example shows how to create an editor for a custom delegate in
33     the model/view framework by reusing a standard Qt editor widget.
34
35     The model/view framework provides a standard delegate that is used by default
36     with the standard view classes. For most purposes, the selection of editor
37     widgets available through this delegate is sufficient for editing text, boolean
38     values, and other simple data types. However, for specific data types, it is
39     sometimes necessary to use a custom delegate to either display the data in a
40     specific way, or allow the user to edit it with a custom control.
41
42     \image spinboxdelegate-example.png
43
44     This concepts behind this example are covered in the
45     \l{Model/View Programming#Delegate Classes}{Delegate Classes} chapter
46     of the \l{Model/View Programming} overview.
47
48     \section1 SpinBoxDelegate Class Definition
49
50     The definition of the delegate is as follows:
51
52     \snippet examples/itemviews/spinboxdelegate/delegate.h 0
53
54     The delegate class declares only those functions that are needed to
55     create an editor widget, display it at the correct location in a view,
56     and communicate with a model. Custom delegates can also provide their
57     own painting code by reimplementing the \c paintEvent() function.
58     Furthermore it is also possible to reuse (and avoid deleting) the editor
59     widget by reimplementing the \a destroyEditor() function. A reused widget
60     could be a mutable member created in the constructor and deleted in
61     the destructor.
62
63     \section1 SpinBoxDelegate Class Implementation
64
65     Delegates are often stateless. The constructor only needs to
66     call the base class's constructor with the parent QObject as its
67     argument:
68
69     \snippet examples/itemviews/spinboxdelegate/delegate.cpp 0
70
71     Since the delegate is a subclass of QItemDelegate, the data it retrieves
72     from the model is displayed in a default style, and we do not need to
73     provide a custom \c paintEvent().
74
75     The \c createEditor() function returns an editor widget, in this case a
76     spin box that restricts values from the model to integers from 0 to 100
77     inclusive.
78
79     \snippet examples/itemviews/spinboxdelegate/delegate.cpp 1
80
81     We install an event filter on the spin box to ensure that it behaves in
82     a way that is consistent with other delegates. The implementation for
83     the event filter is provided by the base class.
84
85     The \c setEditorData() function reads data from the model, converts it
86     to an integer value, and writes it to the editor widget.
87
88     \snippet examples/itemviews/spinboxdelegate/delegate.cpp 2
89
90     Since the view treats delegates as ordinary QWidget instances, we have
91     to use a static cast before we can set the value in the spin box.
92
93     The \c setModelData() function reads the contents of the spin box, and
94     writes it to the model.
95
96     \snippet examples/itemviews/spinboxdelegate/delegate.cpp 3
97
98     We call \l{QSpinBox::interpretText()}{interpretText()} to make sure that
99     we obtain the most up-to-date value in the spin box.
100
101     The \c updateEditorGeometry() function updates the editor widget's
102     geometry using the information supplied in the style option. This is the
103     minimum that the delegate must do in this case.
104
105     \snippet examples/itemviews/spinboxdelegate/delegate.cpp 4
106
107     More complex editor widgets may divide the rectangle available in
108     \c{option.rect} between different child widgets if required.
109
110     \section1 The Main Function
111
112     This example is written in a slightly different way to many of the
113     other examples supplied with Qt. To demonstrate the use of a custom
114     editor widget in a standard view, it is necessary to set up a model
115     containing some arbitrary data and a view to display it.
116
117     We set up the application in the normal way, construct a standard item
118     model to hold some data, set up a table view to use the data in the
119     model, and construct a custom delegate to use for editing:
120
121     \snippet examples/itemviews/spinboxdelegate/main.cpp 0
122
123     The table view is informed about the delegate, and will use it to
124     display each of the items. Since the delegate is a subclass of
125     QItemDelegate, each cell in the table will be rendered using standard
126     painting operations.
127
128     We insert some arbitrary data into the model for demonstration purposes:
129
130     \snippet examples/itemviews/spinboxdelegate/main.cpp 1
131     \snippet examples/itemviews/spinboxdelegate/main.cpp 2
132
133     Finally, the table view is displayed with a window title, and we start
134     the application's event loop:
135
136     \snippet examples/itemviews/spinboxdelegate/main.cpp 3
137
138     Each of the cells in the table can now be edited in the usual way, but
139     the spin box ensures that the data returned to the model is always
140     constrained by the values allowed by the spin box delegate.
141 */