Skip to content

Commit c068da1

Browse files
committed
Doc: Modernize QHash iterator documentation
Promote STL-style iterators as the primary way to iterate over QHash. Replace the detailed explanation with direct guidance to use QHash::asKeyValueRange, and move Java-style iterators to a brief compatibility note. Update snippets to: - Show C++17 structured bindings with std::as_const(hash).asKeyValueRange() (add <utility> header std::as_const). - Add STL-style iterator example for manual control. - Add example for modifying values in place. This makes the docs clearer, const-correct, and aligned with modern C++ usage. Fixes: QTBUG-139662 Pick-to: 6.10.0 6.10 6.9 6.8 Change-Id: Ifb9698b93ca53c3c6a7c82b0f1d393105cd62f35 Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
1 parent c51b2b1 commit c068da1

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <iostream>
77
#include <QDate>
88

9+
#include <utility>
10+
911
using namespace std;
1012

1113
void examples()
@@ -61,11 +63,25 @@ void examples()
6163

6264
{
6365
//! [8]
64-
for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
65-
cout << qPrintable(i.key()) << ": " << i.value() << endl;
66+
for (const auto &[key, value] : std::as_const(hash).asKeyValueRange())
67+
cout << qPrintable(key) << ": " << value << endl;
6668
//! [8]
6769
}
6870

71+
{
72+
//! [qhash-iterator-stl-style]
73+
for (auto it = hash.cbegin(); it != hash.cend(); ++it)
74+
cout << qPrintable(it.key()) << ": " << it.value() << endl;
75+
//! [qhash-iterator-stl-style]
76+
}
77+
78+
{
79+
//! [qhash-iterator-modify-values]
80+
for (auto it = hash.begin(); it != hash.end(); ++it)
81+
it.value() += 1;
82+
//! [qhash-iterator-modify-values]
83+
}
84+
6985
{
7086
//! [9]
7187
hash.insert("plenty", 100);

src/corelib/tools/qhash.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,18 +1529,23 @@ size_t qHash(long double key, size_t seed) noexcept
15291529
QHash will not shrink automatically if items are removed from the
15301530
table. To minimize the memory used by the hash, call squeeze().
15311531
1532-
If you want to navigate through all the (key, value) pairs stored
1533-
in a QHash, you can use an iterator. QHash provides both
1534-
\l{Java-style iterators} (QHashIterator and QMutableHashIterator)
1535-
and \l{STL-style iterators} (QHash::const_iterator and
1536-
QHash::iterator). Here's how to iterate over a QHash<QString,
1537-
int> using a Java-style iterator:
1532+
To iterate through all the (key, value) pairs stored in a
1533+
QHash, use \l {asKeyValueRange}():
15381534
1539-
\snippet code/src_corelib_tools_qhash.cpp 7
1535+
\snippet code/src_corelib_tools_qhash.cpp 8
15401536
1541-
Here's the same code, but using an STL-style iterator:
1537+
This function returns a range object that can be used with structured
1538+
bindings. For manual iterator control, you can also use traditional
1539+
\l{STL-style iterators} (QHash::const_iterator and QHash::iterator):
15421540
1543-
\snippet code/src_corelib_tools_qhash.cpp 8
1541+
\snippet code/src_corelib_tools_qhash.cpp qhash-iterator-stl-style
1542+
1543+
To modify values, use iterators:
1544+
1545+
\snippet code/src_corelib_tools_qhash.cpp qhash-iterator-modify-values
1546+
1547+
QHash also provides \l{Java-style iterators} (QHashIterator and
1548+
QMutableHashIterator) for compatibility.
15441549
15451550
QHash is unordered, so an iterator's sequence cannot be assumed
15461551
to be predictable. If ordering by key is required, use a QMap.

0 commit comments

Comments
 (0)