Documentation: Fix information on how to iterate over lists
Remove the outdated code used for QStringList and point QStringList and QList to the containers page. Task-number: QTBUG-108687 Change-Id: I6fae6410ca759f91da85832ddb9f24e8a0ce202b Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io> (cherry picked from commit 4a6ce541c59cfaabb5c68066d0e19912032e48df) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
24da759c3d
commit
4f0bd0d4f9
@ -22,24 +22,6 @@ Widget::Widget(QWidget *parent)
|
|||||||
fonts << "Courier" << "Verdana";
|
fonts << "Courier" << "Verdana";
|
||||||
//! [0b]
|
//! [0b]
|
||||||
|
|
||||||
//! [1]
|
|
||||||
for (int i = 0; i < fonts.size(); ++i)
|
|
||||||
cout << fonts.at(i).toLocal8Bit().constData() << Qt::endl;
|
|
||||||
//! [1]
|
|
||||||
|
|
||||||
//! [2]
|
|
||||||
QStringListIterator javaStyleIterator(fonts);
|
|
||||||
while (javaStyleIterator.hasNext())
|
|
||||||
cout << javaStyleIterator.next().toLocal8Bit().constData() << Qt::endl;
|
|
||||||
//! [2]
|
|
||||||
|
|
||||||
//! [3]
|
|
||||||
QStringList::const_iterator constIterator;
|
|
||||||
for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd();
|
|
||||||
++constIterator)
|
|
||||||
cout << (*constIterator).toLocal8Bit().constData() << Qt::endl;
|
|
||||||
//! [3]
|
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
QString str = fonts.join(", ");
|
QString str = fonts.join(", ");
|
||||||
// str == "Arial, Helvetica, Times, Courier"
|
// str == "Arial, Helvetica, Times, Courier"
|
||||||
|
@ -84,25 +84,7 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
\section1 Iterating Over the Strings
|
\section1 Iterating Over the Strings
|
||||||
|
|
||||||
To iterate over a list, you can either use index positions or
|
See \l {Iterating over Containers}.
|
||||||
QList's Java-style and STL-style iterator types:
|
|
||||||
|
|
||||||
Indexing:
|
|
||||||
|
|
||||||
\snippet qstringlist/main.cpp 1
|
|
||||||
|
|
||||||
Java-style iterator:
|
|
||||||
|
|
||||||
\snippet qstringlist/main.cpp 2
|
|
||||||
|
|
||||||
STL-style iterator:
|
|
||||||
|
|
||||||
\snippet qstringlist/main.cpp 3
|
|
||||||
|
|
||||||
The QStringListIterator class is simply a type definition for
|
|
||||||
QListIterator<QString>. QStringList also provide the
|
|
||||||
QMutableStringListIterator class which is a type definition for
|
|
||||||
QMutableListIterator<QString>.
|
|
||||||
|
|
||||||
\section1 Manipulating the Strings
|
\section1 Manipulating the Strings
|
||||||
|
|
||||||
|
@ -129,21 +129,7 @@
|
|||||||
support \c operator==(). These requirements are documented on a
|
support \c operator==(). These requirements are documented on a
|
||||||
per-function basis.
|
per-function basis.
|
||||||
|
|
||||||
Like the other container classes, QList provides \l{Java-style iterators}
|
For iterating over the items, see \l {Iterating over Containers}.
|
||||||
(QListIterator and QMutableListIterator) and \l{STL-style iterators}
|
|
||||||
(QList::const_iterator and QList::iterator). In practice, iterators are
|
|
||||||
handy when working with generic algorithms provided by \l{generic
|
|
||||||
algorithms}{Qt} and the C++ standard library. \l{Java-style iterators} are
|
|
||||||
provided for backwards compatibility, prefer \l{STL-style iterators} when
|
|
||||||
writing C++ code.
|
|
||||||
|
|
||||||
\note Iterators over a QList, and references to individual elements
|
|
||||||
within one, cannot be relied on to remain valid when any non-const method
|
|
||||||
of the QList is called. Accessing such an iterator or reference after
|
|
||||||
the call to a non-const method leads to undefined behavior. When stability
|
|
||||||
for iterator-like functionality is required, you should use indexes instead
|
|
||||||
of iterators as they are not tied to QList's internal state and thus do
|
|
||||||
not get invalidated.
|
|
||||||
|
|
||||||
In addition to QList, Qt also provides QVarLengthArray, a very
|
In addition to QList, Qt also provides QVarLengthArray, a very
|
||||||
low-level class with little functionality that is optimized for
|
low-level class with little functionality that is optimized for
|
||||||
|
Loading…
x
Reference in New Issue
Block a user