~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/itemviews-spinboxdelegate.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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 -->
 
6
<head>
 
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>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>
 
21
<p>Files:</p>
 
22
<ul>
 
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>
 
26
</ul>
 
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>&nbsp;   class SpinBoxDelegate : public QItemDelegate
 
34
    {
 
35
        Q_OBJECT
 
36
 
 
37
    public:
 
38
        SpinBoxDelegate(QObject *parent = 0);
 
39
 
 
40
        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option,
 
41
                              const QModelIndex &amp;index) const;
 
42
 
 
43
        void setEditorData(QWidget *editor, const QModelIndex &amp;index) const;
 
44
        void setModelData(QWidget *editor, QAbstractItemModel *model,
 
45
                          const QModelIndex &amp;index) const;
 
46
 
 
47
        void updateEditorGeometry(QWidget *editor,
 
48
            const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const;
 
49
    };</pre>
 
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>&nbsp;   SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
 
55
        : QItemDelegate(parent)
 
56
    {
 
57
    }</pre>
 
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>&nbsp;   QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
 
61
        const QStyleOptionViewItem &amp;/* option */,
 
62
        const QModelIndex &amp;/* index */) const
 
63
    {
 
64
        QSpinBox *editor = new QSpinBox(parent);
 
65
        editor-&gt;setMinimum(0);
 
66
        editor-&gt;setMaximum(100);
 
67
        editor-&gt;installEventFilter(const_cast&lt;SpinBoxDelegate*&gt;(this));
 
68
 
 
69
        return editor;
 
70
    }</pre>
 
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>&nbsp;   void SpinBoxDelegate::setEditorData(QWidget *editor,
 
74
                                        const QModelIndex &amp;index) const
 
75
    {
 
76
        int value = index.model()-&gt;data(index, Qt::DisplayRole).toInt();
 
77
 
 
78
        QSpinBox *spinBox = static_cast&lt;QSpinBox*&gt;(editor);
 
79
        spinBox-&gt;setValue(value);
 
80
    }</pre>
 
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>&nbsp;   void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
 
84
                                       const QModelIndex &amp;index) const
 
85
    {
 
86
        QSpinBox *spinBox = static_cast&lt;QSpinBox*&gt;(editor);
 
87
        spinBox-&gt;interpretText();
 
88
        int value = spinBox-&gt;value();
 
89
 
 
90
        model-&gt;setData(index, value);
 
91
    }</pre>
 
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>&nbsp;   void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
 
95
        const QStyleOptionViewItem &amp;option, const QModelIndex &amp;/* index */) const
 
96
    {
 
97
        editor-&gt;setGeometry(option.rect);
 
98
    }</pre>
 
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>&nbsp;   int main(int argc, char *argv[])
 
105
    {
 
106
        QApplication app(argc, argv);
 
107
 
 
108
        QStandardItemModel *model = new QStandardItemModel(4, 2);
 
109
        QTableView *tableView = new QTableView;
 
110
        tableView-&gt;setModel(model);
 
111
 
 
112
        SpinBoxDelegate *delegate = new SpinBoxDelegate;
 
113
        tableView-&gt;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>&nbsp;       for (int row = 0; row &lt; 4; ++row) {
 
117
            for (int column = 0; column &lt; 2; ++column) {
 
118
                QModelIndex index = model-&gt;index(row, column, QModelIndex());
 
119
                model-&gt;setData(index, QVariant((row+1) * (column+1)));
 
120
            }
 
121
        }</pre>
 
122
<p>Finally, the table view is displayed with a window title, and we start the application's event loop:</p>
 
123
<pre>&nbsp;       tableView-&gt;setWindowTitle(&quot;Spin Box Delegate&quot;);
 
124
        tableView-&gt;show();
 
125
        return app.exec();
 
126
    }</pre>
 
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 &copy; 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>
 
134
</html>