1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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:FDL$
|
---|
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 a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
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 |
|
---|
59 | \section1 SpinBoxDelegate Class Implementation
|
---|
60 |
|
---|
61 | Since the delegate is stateless, the constructor only needs to
|
---|
62 | call the base class's constructor with the parent QObject as its
|
---|
63 | argument:
|
---|
64 |
|
---|
65 | \snippet examples/itemviews/spinboxdelegate/delegate.cpp 0
|
---|
66 |
|
---|
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().
|
---|
70 |
|
---|
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
|
---|
73 | inclusive.
|
---|
74 |
|
---|
75 | \snippet examples/itemviews/spinboxdelegate/delegate.cpp 1
|
---|
76 |
|
---|
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.
|
---|
80 |
|
---|
81 | The \c setEditorData() function reads data from the model, converts it
|
---|
82 | to an integer value, and writes it to the editor widget.
|
---|
83 |
|
---|
84 | \snippet examples/itemviews/spinboxdelegate/delegate.cpp 2
|
---|
85 |
|
---|
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.
|
---|
88 |
|
---|
89 | The \c setModelData() function reads the contents of the spin box, and
|
---|
90 | writes it to the model.
|
---|
91 |
|
---|
92 | \snippet examples/itemviews/spinboxdelegate/delegate.cpp 3
|
---|
93 |
|
---|
94 | We call \l{QSpinBox::interpretText()}{interpretText()} to make sure that
|
---|
95 | we obtain the most up-to-date value in the spin box.
|
---|
96 |
|
---|
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.
|
---|
100 |
|
---|
101 | \snippet examples/itemviews/spinboxdelegate/delegate.cpp 4
|
---|
102 |
|
---|
103 | More complex editor widgets may divide the rectangle available in
|
---|
104 | \c{option.rect} between different child widgets if required.
|
---|
105 |
|
---|
106 | \section1 The Main Function
|
---|
107 |
|
---|
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.
|
---|
112 |
|
---|
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:
|
---|
116 |
|
---|
117 | \snippet examples/itemviews/spinboxdelegate/main.cpp 0
|
---|
118 |
|
---|
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
|
---|
122 | painting operations.
|
---|
123 |
|
---|
124 | We insert some arbitrary data into the model for demonstration purposes:
|
---|
125 |
|
---|
126 | \snippet examples/itemviews/spinboxdelegate/main.cpp 1
|
---|
127 | \snippet examples/itemviews/spinboxdelegate/main.cpp 2
|
---|
128 |
|
---|
129 | Finally, the table view is displayed with a window title, and we start
|
---|
130 | the application's event loop:
|
---|
131 |
|
---|
132 | \snippet examples/itemviews/spinboxdelegate/main.cpp 3
|
---|
133 |
|
---|
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.
|
---|
137 | */
|
---|