1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the documentation of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:FDL$
10 ** GNU Free Documentation License
11 ** Alternatively, this file may be used under the terms of the GNU Free
12 ** Documentation License version 1.3 as published by the Free Software
13 ** Foundation and appearing in the file included in the packaging of
17 ** Alternatively, this file may be used in accordance with the terms
18 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
29 \example itemviews/spinboxdelegate
30 \title Spin Box Delegate Example
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.
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.
42 \image spinboxdelegate-example.png
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.
48 \section1 SpinBoxDelegate Class Definition
50 The definition of the delegate is as follows:
52 \snippet examples/itemviews/spinboxdelegate/delegate.h 0
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.
59 \section1 SpinBoxDelegate Class Implementation
61 Since the delegate is stateless, the constructor only needs to
62 call the base class's constructor with the parent QObject as its
65 \snippet examples/itemviews/spinboxdelegate/delegate.cpp 0
67 Since the delegate is a subclass of QItemDelegate, the data it retrieves
68 from the model is displayed in a default style, and we do not need to
69 provide a custom \c paintEvent().
71 The \c createEditor() function returns an editor widget, in this case a
72 spin box that restricts values from the model to integers from 0 to 100
75 \snippet examples/itemviews/spinboxdelegate/delegate.cpp 1
77 We install an event filter on the spin box to ensure that it behaves in
78 a way that is consistent with other delegates. The implementation for
79 the event filter is provided by the base class.
81 The \c setEditorData() function reads data from the model, converts it
82 to an integer value, and writes it to the editor widget.
84 \snippet examples/itemviews/spinboxdelegate/delegate.cpp 2
86 Since the view treats delegates as ordinary QWidget instances, we have
87 to use a static cast before we can set the value in the spin box.
89 The \c setModelData() function reads the contents of the spin box, and
90 writes it to the model.
92 \snippet examples/itemviews/spinboxdelegate/delegate.cpp 3
94 We call \l{QSpinBox::interpretText()}{interpretText()} to make sure that
95 we obtain the most up-to-date value in the spin box.
97 The \c updateEditorGeometry() function updates the editor widget's
98 geometry using the information supplied in the style option. This is the
99 minimum that the delegate must do in this case.
101 \snippet examples/itemviews/spinboxdelegate/delegate.cpp 4
103 More complex editor widgets may divide the rectangle available in
104 \c{option.rect} between different child widgets if required.
106 \section1 The Main Function
108 This example is written in a slightly different way to many of the
109 other examples supplied with Qt. To demonstrate the use of a custom
110 editor widget in a standard view, it is necessary to set up a model
111 containing some arbitrary data and a view to display it.
113 We set up the application in the normal way, construct a standard item
114 model to hold some data, set up a table view to use the data in the
115 model, and construct a custom delegate to use for editing:
117 \snippet examples/itemviews/spinboxdelegate/main.cpp 0
119 The table view is informed about the delegate, and will use it to
120 display each of the items. Since the delegate is a subclass of
121 QItemDelegate, each cell in the table will be rendered using standard
124 We insert some arbitrary data into the model for demonstration purposes:
126 \snippet examples/itemviews/spinboxdelegate/main.cpp 1
127 \snippet examples/itemviews/spinboxdelegate/main.cpp 2
129 Finally, the table view is displayed with a window title, and we start
130 the application's event loop:
132 \snippet examples/itemviews/spinboxdelegate/main.cpp 3
134 Each of the cells in the table can now be edited in the usual way, but
135 the spin box ensures that the data returned to the model is always
136 constrained by the values allowed by the spin box delegate.