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

« back to all changes in this revision

Viewing changes to doc/html/model-view-convenience.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/model-view-programming.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Item View Convenience Classes</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
    <link rel="prev" href="model-view-delegate.html" />
 
15
    <link rel="contents" href="model-view-programming.html" />
 
16
</head>
 
17
<body>
 
18
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
19
<tr>
 
20
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
21
<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>
 
22
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
23
[Previous: <a href="model-view-delegate.html">Delegate Classes</a>]
 
24
[<a href="model-view-programming.html">Contents</a>]
 
25
</p>
 
26
<h1 align="center">Item View Convenience Classes</h1>
 
27
<ul><li><a href="#overview">Overview</a></li>
 
28
<li><a href="#list-widgets">List Widgets</a></li>
 
29
<li><a href="#tree-widgets">Tree Widgets</a></li>
 
30
<li><a href="#table-widgets">Table Widgets</a></li>
 
31
<li><a href="#common-features">Common Features</a></li>
 
32
<ul><li><a href="#hidden-items">Hidden Items</a></li>
 
33
<li><a href="#selections">Selections</a></li>
 
34
<li><a href="#searching">Searching</a></li>
 
35
</ul>
 
36
</ul>
 
37
<a name="overview"></a>
 
38
<h2>Overview</h2>
 
39
<p>Alongside the model/view classes, Qt 4 also includes standard widgets to provide classic item-based container widgets. These behave in a similar way to the item view classes in Qt 3, but have been rewritten to use the underlying model/view framework for performance and maintainability. The old item view classes are still available in the compatibility library (see the <a href="porting4.html">Porting Guide</a> for more information).</p>
 
40
<p>The item-based widgets have been given names which reflect their uses: <tt>QListWidget</tt> provides a list of items, <tt>QTreeWidget</tt> displays a multi-level tree structure, and <tt>QTableWidget</tt> provides a table of cell items. Each class inherits the behavior of the <tt>QAbstractItemView</tt> class which implements common behavior for item selection and header management.</p>
 
41
<a name="list-widgets"></a>
 
42
<h2>List Widgets</h2>
 
43
<p>Single level lists of items are typically displayed using a <tt>QListWidget</tt> and a number of <tt>QListWidgetItem</tt>s. A list widget is constructed in the same way as any other widget:</p>
 
44
<pre>&nbsp;       QListWidget *listWidget;
 
45
        listWidget = new QListWidget(this);</pre>
 
46
<p>List items can be added directly to the list widget when they are constructed:</p>
 
47
<pre>&nbsp;       new QListWidgetItem(tr(&quot;Sycamore&quot;), listWidget);
 
48
        new QListWidgetItem(tr(&quot;Chestnut&quot;), listWidget);
 
49
        new QListWidgetItem(tr(&quot;Mahogany&quot;), listWidget);</pre>
 
50
<p>They can also be constructed without a parent list widget and added to a list at some later time:</p>
 
51
<pre>&nbsp;       QListWidgetItem *newItem = new QListWidgetItem;
 
52
        newItem-&gt;setText(itemText);
 
53
        listWidget-&gt;insertItem(row, newItem);</pre>
 
54
<p>Each item in a list can display a text label and an icon. The colors and font used to render the text can be changed to provide a customized appearance for items. Tooltips, status tips, and &quot;What's This?&quot; help are all easily configured to ensure that the list is properly integrated into the application.</p>
 
55
<pre>&nbsp;       newItem-&gt;setToolTip(toolTipText);
 
56
        newItem-&gt;setStatusTip(toolTipText);
 
57
        newItem-&gt;setWhatsThis(whatsThisText);</pre>
 
58
<p>By default, items in a list are presented in the order of their creation. Lists of items can be sorted according to the criteria given in <a href="qt.html#SortOrder-enum">Qt::SortOrder</a> to produce a list of items that is sorted in forward or reverse alphabetical order:</p>
 
59
<pre>&nbsp;       listWidget-&gt;sortItems(Qt::AscendingOrder);
 
60
        listWidget-&gt;sortItems(Qt::DescendingOrder);</pre>
 
61
<a name="tree-widgets"></a>
 
62
<h2>Tree Widgets</h2>
 
63
<p>Trees or hierarchical lists of items are provided by the <tt>QTreeWidget</tt> and <tt>QTreeWidgetItem</tt> classes. Each item in the tree widget can have child items of its own, and can display a number of columns of information. Tree widgets are created just like any other widget:</p>
 
64
<pre>&nbsp;       QTreeWidget *treeWidget;
 
65
        treeWidget = new QTreeWidget(this);</pre>
 
66
<p>Before items can be added to the tree widget, the number of columns must be set. For example, we could define two columns, and create a header to provide labels at the top of each column:</p>
 
67
<pre>&nbsp;       treeWidget-&gt;setColumnCount(2);
 
68
        QStringList headers;
 
69
        headers &lt;&lt; tr(&quot;Subject&quot;) &lt;&lt; tr(&quot;Default&quot;);
 
70
        treeWidget-&gt;setHeaderLabels(headers);</pre>
 
71
<p>The easiest way to set up the labels for each section is to supply a string list. For more sophisticated headers, you can construct a tree item, decorate it as you wish, and use that as the tree widget's header.</p>
 
72
<p>Top-level items in the tree widget are constructed with the tree widget as their parent widget. They can be inserted in an arbitrary order, or you can ensure that they are listed in a particular order by specifying the previous item when constructing each item:</p>
 
73
<pre>&nbsp;       QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);
 
74
        cities-&gt;setText(0, tr(&quot;Cities&quot;));
 
75
        QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);
 
76
        osloItem-&gt;setText(0, tr(&quot;Oslo&quot;));
 
77
        osloItem-&gt;setText(1, tr(&quot;Yes&quot;));
 
78
 
 
79
        QTreeWidgetItem *planets = new QTreeWidgetItem(treeWidget, cities);</pre>
 
80
<p>Tree widgets deal with top-level items slightly differently to other items from deeper within the tree. Items can be removed from the top level of the tree by calling the tree widget's <a href="qtreewidget.html#takeTopLevelItem">takeTopLevelItem()</a> function, but items from lower levels are removed by calling their parent item's <a href="qtreewidgetitem.html#takeChild">takeChild()</a> function. Items are inserted in the top level of the tree with the <a href="qtreewidget.html#insertTopLevelItem">insertTopLevelItem()</a> function. At lower levels in the tree, the parent item's <a href="qtreewidgetitem.html#insertChild">insertChild()</a> function is used.</p>
 
81
<p>It is easy to move items around between the top level and lower levels in the tree. We just need to check whether the items are top-level items or not, and this information is supplied by each item's <tt>parent()</tt> function. For example, we can remove the current item in the tree widget regardless of its location:</p>
 
82
<pre>&nbsp;       QTreeWidgetItem *parent = currentItem-&gt;parent();
 
83
        int index;
 
84
 
 
85
        if (parent) {
 
86
            index = parent-&gt;indexOfChild(treeWidget-&gt;currentItem());
 
87
            delete parent-&gt;takeChild(index);
 
88
        } else {
 
89
            index = treeWidget-&gt;indexOfTopLevelItem(treeWidget-&gt;currentItem());
 
90
            delete treeWidget-&gt;takeTopLevelItem(index);
 
91
        }</pre>
 
92
<p>Inserting the item somewhere else in the tree widget follows the same pattern:</p>
 
93
<pre>&nbsp;       QTreeWidgetItem *parent = currentItem-&gt;parent();
 
94
        QTreeWidgetItem *newItem;
 
95
        if (parent)
 
96
            newItem = new QTreeWidgetItem(parent, treeWidget-&gt;currentItem());
 
97
        else
 
98
            newItem = new QTreeWidgetItem(treeWidget, treeWidget-&gt;currentItem());</pre>
 
99
<a name="table-widgets"></a>
 
100
<h2>Table Widgets</h2>
 
101
<p>Tables of items similar to those found in spreadsheet applications are constructed with the <tt>QTableWidget</tt> and <tt>QTableWidgetItem</tt>. These provide a scrolling table widget with headers and items to use within it.</p>
 
102
<p>Tables can be created with a set number of rows and columns, or these can be added to an unsized table as they are needed.</p>
 
103
<pre>&nbsp;       QTableWidget *tableWidget;
 
104
        tableWidget = new QTableWidget(12, 3, this);</pre>
 
105
<p>Items are constructed outside the table before being added to the table at the required location:</p>
 
106
<pre>&nbsp;       QTableWidgetItem *newItem = new QTableWidgetItem(tr(&quot;%1&quot;).arg(
 
107
            pow(row, column+1)));
 
108
        tableWidget-&gt;setItem(row, column, newItem);</pre>
 
109
<p>Horizontal and vertical headers can be added to the table by constructing items outside the table and using them as headers:</p>
 
110
<pre>&nbsp;       QTableWidgetItem *valuesHeaderItem = new QTableWidgetItem(tr(&quot;Values&quot;));
 
111
        tableWidget-&gt;setHorizontalHeaderItem(0, valuesHeaderItem);</pre>
 
112
<p>Note that the rows and columns in the table begin at zero.</p>
 
113
<a name="common-features"></a>
 
114
<h2>Common Features</h2>
 
115
<p>There are a number of item-based features common to each of the convenience classes that are available through the same interfaces in each class. We present these in the following sections with some examples for different widgets. Look at the list of <a href="model-view.html">Model/View Classes</a> for each of the widgets for more details about the use of each function used.</p>
 
116
<a name="hidden-items"></a>
 
117
<h3>Hidden Items</h3>
 
118
<p>It is sometimes useful to be able to hide items in an item view widget rather than remove them. Items for all of the above widgets can be hidden and later shown again. You can determine whether an item is hidden by calling the isItemHidden() function, and items can be hidden with <tt>setItemHidden()</tt>.</p>
 
119
<p>Since this operation is item-based, the same function is available for all three convenience classes.</p>
 
120
<a name="selections"></a>
 
121
<h3>Selections</h3>
 
122
<p>The way items are selected is controlled by the widget's selection mode (<a href="qabstractitemview.html#SelectionMode-enum">QAbstractItemView::SelectionMode</a>). This property controls whether the user can select one or many items and, in many-item selections, whether the selection must be a continuous range of items. The selection mode works in the same way for all of the above widgets.</p>
 
123
<table align="center" cellpadding="2" cellspacing="1" border="0">
 
124
<tr valign="top" bgcolor="#f0f0f0"><td><center><img src="images/selection-single.png" /></center></td><td><b>Single item selections:</b> Where the user needs to choose a single item from a widget, the default <tt>SingleSelection</tt> mode is most suitable. In this mode, the current item and the selected item are the same.</td></tr>
 
125
<tr valign="top" bgcolor="#e0e0e0"><td><center><img src="images/selection-multi.png" /></center></td><td><b>Multi-item selections:</b> In this mode, the user can toggle the selection state of any item in the widget without changing the existing selection, much like the way non-exclusive checkboxes can be toggled independently.</td></tr>
 
126
<tr valign="top" bgcolor="#f0f0f0"><td><center><img src="images/selection-extended.png" /></center></td><td><b>Extended selections:</b> Widgets that often require many adjacent items to be selected, such as those found in spreadsheets, require the <tt>ExtendedSelection</tt> mode. In this mode, continuous ranges of items in the widget can be selected with both the mouse and the keyboard. Complex selections, involving many items that are not adjacent to other selected items in the widget, can also be created if modifier keys are used.<p>If the user selects an item without using a modifier key, the existing selection is cleared.</p>
 
127
</td></tr>
 
128
</table>
 
129
<p>The selected items in a widget are read using the <tt>selectedItems()</tt> function, providing a list of relevant items that can be iterated over. For example, we can find the sum of all the numeric values within a list of selected items with the following code:</p>
 
130
<pre>&nbsp;       QList&lt;QTableWidgetItem *&gt; selected = tableWidget-&gt;selectedItems();
 
131
        QTableWidgetItem *item;
 
132
        int number = 0;
 
133
        double total = 0;
 
134
 
 
135
        foreach (item, selected) {
 
136
            bool ok;
 
137
            double value = item-&gt;text().toDouble(&amp;ok);
 
138
 
 
139
            if (ok &amp;&amp; !item-&gt;text().isEmpty()) {
 
140
                total += value;
 
141
                number++;
 
142
            }
 
143
        }</pre>
 
144
<p>Note that for the single selection mode, the current item will be in the selection. In the multi-selection and extended selection modes, the current item may not lie within the selection, depending on the way the user formed the selection.</p>
 
145
<a name="searching"></a>
 
146
<h3>Searching</h3>
 
147
<p>It is often useful to be able to find items within an item view widget, either as a developer or as a service to present to users. All three item view convenience classes provide a common <tt>findItems()</tt> function to make this as consistent and simple as possible.</p>
 
148
<p>Items are searched for by the text that they contain according to criteria specified by a selection of values from <a href="qt.html#MatchFlag-enum">Qt::MatchFlags</a>. We can obtain a list of matching items with the <tt>findItems()</tt> function:</p>
 
149
<pre>&nbsp;       QTreeWidgetItem *item;
 
150
        QList&lt;QTreeWidgetItem *&gt; found = treeWidget-&gt;findItems(
 
151
            itemText, Qt::MatchWildcard);
 
152
 
 
153
        foreach (item, found) {
 
154
            treeWidget-&gt;setItemSelected(item, true);
 
155
            // Show the item-&gt;text(0) for each item.
 
156
        }</pre>
 
157
<p>The above code causes items in a tree widget to be selected if they contain the text given in the search string. This pattern can also be used in the list and table widgets.</p>
 
158
<p>
 
159
[Previous: <a href="model-view-delegate.html">Delegate Classes</a>]
 
160
[<a href="model-view-programming.html">Contents</a>]
 
161
</p>
 
162
<p /><address><hr /><div align="center">
 
163
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
164
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
165
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
166
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
167
</tr></table></div></address></body>
 
168
</html>