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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Toolkit - QListIterator Class</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<h1 align=center>QListIterator Class Reference</h1><br clear="all">
<p>
The QListIterator class provides an iterator for <a href="qlist.html">QList</a> collections.
<a href="#details">More...</a>
<p>
<code>#include <<a href="qlist-h.html">qlist.h</a>></code>
<p>
Inherits <a href="qglistiterator.html">QGListIterator</a>.
<p>Inherited by <a href="qstrlistiterator.html">QStrListIterator</a>.
<p><a href="qlistiterator-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li><div class="fn"><a href="#3632b5"><b>QListIterator</b></a>(constQList<type>&list)</div>
<li><div class="fn"><a href="#fa842a"><b>~QListIterator</b></a>()</div>
<li><div class="fn">uint<a href="#e61e8b"><b>count</b></a>()const</div>
<li><div class="fn">bool<a href="#58bf87"><b>isEmpty</b></a>()const</div>
<li><div class="fn">bool<a href="#d97479"><b>atFirst</b></a>()const</div>
<li><div class="fn">bool<a href="#18e97b"><b>atLast</b></a>()const</div>
<li><div class="fn">type*<a href="#1c3329"><b>toFirst</b></a>()</div>
<li><div class="fn">type*<a href="#70faa8"><b>toLast</b></a>()</div>
<li><div class="fn">operator<a href=qlistiterator.html#d059ee><b>type*</b></a>()const</div>
<li><div class="fn">type*<a href="#79f49b"><b>operator*</b></a>()</div>
<li><div class="fn">type*<a href="#e58f06"><b>current</b></a>()const</div>
<li><div class="fn">type*<a href="#5e2774"><b>operator</b></a>()</div>
<li><div class="fn">type*<a href="#b0fa92"><b>operator++</b></a>()</div>
<li><div class="fn">type*<a href="#29ff97"><b>operator+=</b></a>(uintjump)</div>
<li><div class="fn">type*<a href="#c55dc1"><b>operator--</b></a>()</div>
<li><div class="fn">type*<a href="#b50d5d"><b>operator-=</b></a>(uintjump)</div>
<li><div class="fn">QListIterator<type>&<a href="#21ae66"><b>operator=</b></a>(constQListIterator<type>&it)</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QListIterator class provides an iterator for <a href="qlist.html">QList</a> collections.
<p>
Define a template instance QListIterator<X> to create a list iterator
that operates on QList<X> (list of X*).
<p>Example:
<pre> #include <qlist.h>
#include <qstring.h>
#include <stdio.h>
class Employee
{
public:
Employee( const char *name, int salary ) { n=name; s=salary; }
const char *name() const { return n; }
int salary() const { return s; }
private:
<a href="qstring.html">QString</a> n;
int s;
};
void main()
{
<a href="qlist.html">QList</a><Employee> list; // list of pointers to Employee
list.<a href="qcollection.html#a8ef9f">setAutoDelete</a>( TRUE ); // delete items when they are removed
list.<a href="qlist.html#c5746a">append</a>( new Employee("Bill", 50000) );
list.<a href="qlist.html#c5746a">append</a>( new Employee("Steve",80000) );
list.<a href="qlist.html#c5746a">append</a>( new Employee("Ron", 60000) );
<a href="qlistiterator.html">QListIterator</a><Employee> it(list); // iterator for employee list
for ( ; it.<a href="#e58f06">current</a>(); ++it ) {
Employee *emp = it.<a href="#e58f06">current</a>();
printf( "%s earns %d\n", emp->name().latin1(), emp->salary() );
}
}
</pre>
<p>Program output:
<pre> Bill earns 50000
Steve earns 80000
Ron earns 60000
</pre>
<p>Although QList has member functions to traverse the doubly linked list
structure, using a list iterator is a much more robust way of traversing
the list, because multiple list iterators can operate on the same list,
independent of each other and independent of the QList's current item.
An iterator has its own current list item and can get the next and
previous list items. It can only traverse the list, never modify it.
<p>A QList knows about all list iterators that are operating on the list.
When an item is removed from the list, the list update all iterators
that are pointing the removed item to point to the new current list item.
<p>Example:
<pre> #include <qlist.h>
#include <qstring.h>
#include <stdio.h>
class Employee
{
... // same as above
};
void main()
{
<a href="qlist.html">QList</a><Employee> list; // list of pointers to Employee
list.<a href="qcollection.html#a8ef9f">setAutoDelete</a>( TRUE ); // delete items when they are removed
list.<a href="qlist.html#c5746a">append</a>( new Employee("Bill", 50000) );
list.<a href="qlist.html#c5746a">append</a>( new Employee("Steve",80000) );
list.<a href="qlist.html#c5746a">append</a>( new Employee("Ron", 60000) );
<a href="qlistiterator.html">QListIterator</a><Employee> it(list);
list.<a href="qlist.html#0e7e42">at</a>( 1 ); // current list item: "Steve"
it.<a href="#70faa8">toLast</a>(); // it: "Ron"
--it; // it: "Steve"
// Now, both the list and the iterator are referring the same item
list.<a href="qlist.html#00d619">remove</a>();
printf( "%s\n", it.<a href="#e58f06">current</a>()->name().latin1() );
}
</pre>
<p>Program output:
<pre> Ron
</pre>
<p>See also <a href="qlist.html">QList</a> and <a href="collection.html">collection classes</a>
<p>Examples:
<a href="dirview-main-cpp.html#QListIterator">dirview/main.cpp</a>
<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="3632b5"></a>QListIterator::QListIterator(const<a href="qlist.html">QList</a><type>&list)</h3>
<p>Constructs an iterator for <em>list.</em> The current iterator item is
set to point on the first item in the <em>list.</em>
<h3 class="fn"><a name="fa842a"></a>QListIterator::~QListIterator()</h3>
<p>Destroys the iterator.
<h3 class="fn"><a name="d059ee"></a>QListIterator::operatortype*()const</h3>
<p>Cast operator. Returns a pointer to the current iterator item.
Same as <a href="#e58f06">current</a>().
<h3 class="fn">bool<a name="d97479"></a>QListIterator::atFirst()const</h3>
<p>Returns TRUE if the current iterator item is the first list item, otherwise
FALSE.
<p>See also <a href="#1c3329">toFirst</a>() and <a href="#18e97b">atLast</a>().
<h3 class="fn">bool<a name="18e97b"></a>QListIterator::atLast()const</h3>
<p>Returns TRUE if the current iterator item is the last list item, otherwise
FALSE.
<p>See also <a href="#70faa8">toLast</a>() and <a href="#d97479">atFirst</a>().
<h3 class="fn">uint<a name="e61e8b"></a>QListIterator::count()const</h3>
<p>Returns the number of items in the list this iterator operates on.
<p>See also <a href="#58bf87">isEmpty</a>().
<h3 class="fn">type*<a name="e58f06"></a>QListIterator::current()const</h3>
<p>Returns a pointer to the current iterator item.
<h3 class="fn">bool<a name="58bf87"></a>QListIterator::isEmpty()const</h3>
<p>Returns TRUE if the list is empty, i.e. <a href="#e61e8b">count</a>() == 0, otherwise FALSE.
<p>See also <a href="#e61e8b">count</a>().
<h3 class="fn">type*<a name="5e2774"></a>QListIterator::operator()()</h3>
<p>Makes the succeeding item current and returns the original current item.
<p>If the current iterator item was the last item in the list or if it was
null, null is returned.
<h3 class="fn">type*<a name="79f49b"></a>QListIterator::operator*()</h3>
<p>Asterix operator. Returns a pointer to the current iterator item.
Same as <a href="#e58f06">current</a>().
<h3 class="fn">type*<a name="b0fa92"></a>QListIterator::operator++()</h3>
<p>Prefix ++ makes the succeeding item current and returns the new current
item.
<p>If the current iterator item was the last item in the list or if it was
null, null is returned.
<h3 class="fn">type*<a name="29ff97"></a>QListIterator::operator+=(uintjump)</h3>
<p>Sets the current item to the item <em>jump</em> positions after the current item,
and returns a pointer to that item.
<p>If that item is beyond the last item or if the dictionary is empty,
it sets the current item to null and returns null.
<h3 class="fn">type*<a name="c55dc1"></a>QListIterator::operator--()</h3>
<p>Prefix -- makes the preceding item current and returns the new current
item.
<p>If the current iterator item was the first item in the list or if it was
null, null is returned.
<h3 class="fn">type*<a name="b50d5d"></a>QListIterator::operator-=(uintjump)</h3>
<p>Returns the item <em>jump</em> positions before the current item, or null if
it is beyond the first item. Makes this the current item.
<h3 class="fn">QListIterator<type>&<a name="21ae66"></a>QListIterator::operator=(constQListIterator<type>&it)</h3>
<p>Assignment. Makes a copy of the iterator <em>it</em> and returns a reference
to this iterator.
<h3 class="fn">type*<a name="1c3329"></a>QListIterator::toFirst()</h3>
<p>Sets the current iterator item to point to the first list item and returns
a pointer to the item. Sets the current item to null and returns null
if the list is empty.
<p>See also <a href="#70faa8">toLast</a>() and <a href="#d97479">atFirst</a>().
<h3 class="fn">type*<a name="70faa8"></a>QListIterator::toLast()</h3>
<p>Sets the current iterator item to point to the last list item and returns
a pointer to the item. Sets the current item to null and returns null
if the list is empty.
<p>See also <a href="#1c3329">toFirst</a>() and <a href="#18e97b">atLast</a>().
<hr><p>
Search the documentation, FAQ, qt-interest archive and more (uses
<a href="http://www.trolltech.com">www.trolltech.com</a>):<br>
<form method=post action="http://www.trolltech.com/search.cgi">
<input type=hidden name="version" value="2.3.2"><nobr>
<input size="50" name="search"><input type=submit value="Search">
</nobr></form><hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>,
copyright © 1995-2001
<a href="http://www.trolltech.com">Trolltech</a>, all rights reserved.<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|