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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/spinboxdelegate.qdoc -->
<head>
    <title>Qt 4.0: Spin Box Delegate</title>
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
<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>
<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>
<p>Files:</p>
<ul>
<li><a href="itemviews-spinboxdelegate-delegate-cpp.html">itemviews/spinboxdelegate/delegate.cpp</a></li>
<li><a href="itemviews-spinboxdelegate-delegate-h.html">itemviews/spinboxdelegate/delegate.h</a></li>
<li><a href="itemviews-spinboxdelegate-main-cpp.html">itemviews/spinboxdelegate/main.cpp</a></li>
</ul>
<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>
<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>
<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>
<a name="spinboxdelegate-class-definition"></a>
<h2>SpinBoxDelegate Class Definition</h2>
<p>The definition of the delegate is as follows:</p>
<pre>&nbsp;   class SpinBoxDelegate : public QItemDelegate
    {
        Q_OBJECT

    public:
        SpinBoxDelegate(QObject *parent = 0);

        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option,
                              const QModelIndex &amp;index) const;

        void setEditorData(QWidget *editor, const QModelIndex &amp;index) const;
        void setModelData(QWidget *editor, QAbstractItemModel *model,
                          const QModelIndex &amp;index) const;

        void updateEditorGeometry(QWidget *editor,
            const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const;
    };</pre>
<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>
<a name="spinboxdelegate-class-implementation"></a>
<h2>SpinBoxDelegate Class Implementation</h2>
<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>
<pre>&nbsp;   SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
        : QItemDelegate(parent)
    {
    }</pre>
<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>
<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>
<pre>&nbsp;   QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &amp;/* option */,
        const QModelIndex &amp;/* index */) const
    {
        QSpinBox *editor = new QSpinBox(parent);
        editor-&gt;setMinimum(0);
        editor-&gt;setMaximum(100);
        editor-&gt;installEventFilter(const_cast&lt;SpinBoxDelegate*&gt;(this));

        return editor;
    }</pre>
<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>
<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>
<pre>&nbsp;   void SpinBoxDelegate::setEditorData(QWidget *editor,
                                        const QModelIndex &amp;index) const
    {
        int value = index.model()-&gt;data(index, Qt::DisplayRole).toInt();

        QSpinBox *spinBox = static_cast&lt;QSpinBox*&gt;(editor);
        spinBox-&gt;setValue(value);
    }</pre>
<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>
<p>The <tt>setModelData()</tt> function reads the contents of the spin box, and writes it to the model.</p>
<pre>&nbsp;   void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                       const QModelIndex &amp;index) const
    {
        QSpinBox *spinBox = static_cast&lt;QSpinBox*&gt;(editor);
        spinBox-&gt;interpretText();
        int value = spinBox-&gt;value();

        model-&gt;setData(index, value);
    }</pre>
<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>
<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>
<pre>&nbsp;   void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &amp;option, const QModelIndex &amp;/* index */) const
    {
        editor-&gt;setGeometry(option.rect);
    }</pre>
<p>More complex editor widgets may divide the rectangle available in <tt>option.rect</tt> between different child widgets if required.</p>
<a name="the-main-function"></a>
<h2>The Main Function</h2>
<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>
<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>
<pre>&nbsp;   int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        QStandardItemModel *model = new QStandardItemModel(4, 2);
        QTableView *tableView = new QTableView;
        tableView-&gt;setModel(model);

        SpinBoxDelegate *delegate = new SpinBoxDelegate;
        tableView-&gt;setItemDelegate(delegate);</pre>
<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>
<p>We insert some arbitrary data into the model for demonstration purposes:</p>
<pre>&nbsp;       for (int row = 0; row &lt; 4; ++row) {
            for (int column = 0; column &lt; 2; ++column) {
                QModelIndex index = model-&gt;index(row, column, QModelIndex());
                model-&gt;setData(index, QVariant((row+1) * (column+1)));
            }
        }</pre>
<p>Finally, the table view is displayed with a window title, and we start the application's event loop:</p>
<pre>&nbsp;       tableView-&gt;setWindowTitle(&quot;Spin Box Delegate&quot;);
        tableView-&gt;show();
        return app.exec();
    }</pre>
<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>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
</tr></table></div></address></body>
</html>