1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/spinboxdelegate.qdoc -->
7
<title>Qt 4.0: Spin Box Delegate</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Spin Box Delegate</h1>
23
<li><a href="itemviews-spinboxdelegate-delegate-cpp.html">itemviews/spinboxdelegate/delegate.cpp</a></li>
24
<li><a href="itemviews-spinboxdelegate-delegate-h.html">itemviews/spinboxdelegate/delegate.h</a></li>
25
<li><a href="itemviews-spinboxdelegate-main-cpp.html">itemviews/spinboxdelegate/main.cpp</a></li>
27
<p>The Spin Box Delegate example shows how to create an editor for a custom delegate in the model/view framework by reusing a standard Qt editor widget.</p>
28
<p>The model/view framework provides a standard delegate that is used by default with the standard view classes. For most purposes, the selection of editor widgets available through this delegate is sufficient for editing text, boolean values, and other simple data types. However, for specific data types, it is sometimes necessary to use a custom delegate to either display the data in a specific way, or allow the user to edit it with a custom control.</p>
29
<center><img src="images/spinboxdelegate-example.png" /></center><p>This concepts behind this example are covered in the <a href="model-view-delegate.html">Delegate Classes</a> chapter of the <a href="model-view-programming.html">Model/View Programming</a> overview.</p>
30
<a name="spinboxdelegate-class-definition"></a>
31
<h2>SpinBoxDelegate Class Definition</h2>
32
<p>The definition of the delegate is as follows:</p>
33
<pre> class SpinBoxDelegate : public QItemDelegate
38
SpinBoxDelegate(QObject *parent = 0);
40
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
41
const QModelIndex &index) const;
43
void setEditorData(QWidget *editor, const QModelIndex &index) const;
44
void setModelData(QWidget *editor, QAbstractItemModel *model,
45
const QModelIndex &index) const;
47
void updateEditorGeometry(QWidget *editor,
48
const QStyleOptionViewItem &option, const QModelIndex &index) const;
50
<p>The delegate class declares only those functions that are needed to create an editor widget, display it at the correct location in a view, and communicate with a model. Custom delegates can also provide their own painting code by reimplementing the <tt>paintEvent()</tt> function.</p>
51
<a name="spinboxdelegate-class-implementation"></a>
52
<h2>SpinBoxDelegate Class Implementation</h2>
53
<p>Since the delegate is stateless, the constructor only needs to call the base class's constructor with the parent <a href="qobject.html">QObject</a> as its argument:</p>
54
<pre> SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
55
: QItemDelegate(parent)
58
<p>Since the delegate is a subclass of <a href="qitemdelegate.html">QItemDelegate</a>, the data it retrieves from the model is displayed in a default style, and we do not need to provide a custom <tt>paintEvent()</tt>.</p>
59
<p>The <tt>createEditor()</tt> function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive.</p>
60
<pre> QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
61
const QStyleOptionViewItem &/* option */,
62
const QModelIndex &/* index */) const
64
QSpinBox *editor = new QSpinBox(parent);
65
editor->setMinimum(0);
66
editor->setMaximum(100);
67
editor->installEventFilter(const_cast<SpinBoxDelegate*>(this));
71
<p>We install an event filter on the spin box to ensure that it behaves in a way that is consistent with other delegates. The implementation for the event filter is provided by the base class.</p>
72
<p>The <tt>setEditorData()</tt> function reads data from the model, converts it to an integer value, and writes it to the editor widget.</p>
73
<pre> void SpinBoxDelegate::setEditorData(QWidget *editor,
74
const QModelIndex &index) const
76
int value = index.model()->data(index, Qt::DisplayRole).toInt();
78
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
79
spinBox->setValue(value);
81
<p>Since the view treats delegates as ordinary <a href="qwidget.html">QWidget</a> instances, we have to use a static cast before we can set the value in the spin box.</p>
82
<p>The <tt>setModelData()</tt> function reads the contents of the spin box, and writes it to the model.</p>
83
<pre> void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
84
const QModelIndex &index) const
86
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
87
spinBox->interpretText();
88
int value = spinBox->value();
90
model->setData(index, value);
92
<p>We call <a href="qabstractspinbox.html#interpretText">interpretText()</a> to make sure that we obtain the most up-to-date value in the spin box.</p>
93
<p>The <tt>updateEditorGeometry()</tt> function updates the editor widget's geometry using the information supplied in the style option. This is the minimum that the delegate must do in this case.</p>
94
<pre> void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
95
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
97
editor->setGeometry(option.rect);
99
<p>More complex editor widgets may divide the rectangle available in <tt>option.rect</tt> between different child widgets if required.</p>
100
<a name="the-main-function"></a>
101
<h2>The Main Function</h2>
102
<p>This example is written in a slightly different way to many of the other examples supplied with Qt. To demonstrate the use of a custom editor widget in a standard view, it is necessary to set up a model containing some arbitrary data and a view to display it.</p>
103
<p>We set up the application in the normal way, construct a standard item model to hold some data, set up a table view to use the data in the model, and construct a custom delegate to use for editing:</p>
104
<pre> int main(int argc, char *argv[])
106
QApplication app(argc, argv);
108
QStandardItemModel *model = new QStandardItemModel(4, 2);
109
QTableView *tableView = new QTableView;
110
tableView->setModel(model);
112
SpinBoxDelegate *delegate = new SpinBoxDelegate;
113
tableView->setItemDelegate(delegate);</pre>
114
<p>The table view is informed about the delegate, and will use it to display each of the items. Since the delegate is a subclass of <a href="qitemdelegate.html">QItemDelegate</a>, each cell in the table will be rendered using standard painting operations.</p>
115
<p>We insert some arbitrary data into the model for demonstration purposes:</p>
116
<pre> for (int row = 0; row < 4; ++row) {
117
for (int column = 0; column < 2; ++column) {
118
QModelIndex index = model->index(row, column, QModelIndex());
119
model->setData(index, QVariant((row+1) * (column+1)));
122
<p>Finally, the table view is displayed with a window title, and we start the application's event loop:</p>
123
<pre> tableView->setWindowTitle("Spin Box Delegate");
124
tableView->show();
127
<p>Each of the cells in the table can now be edited in the usual way, but the spin box ensures that the data returned to the model is always constrained by the values allowed by the spin box delegate.</p>
128
<p /><address><hr /><div align="center">
129
<table width="100%" cellspacing="0" border="0"><tr class="address">
130
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
131
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
132
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
133
</tr></table></div></address></body>