The Qt documentation recommend an iterator-based solution to iterate over an associative container like QMap and QHash, and I always wondered if there really isn't a (nice) solution using a foreach
loop like in PHP:
foreach($container as $key => $value)
I looked into the source code of the foreach
macro and extended it by a key variable. The following code is only the GCC version of the loop (it doesn't work with all compilers, see Q_FOREACH
macro definition in qglobal.h
for other versions).
#define foreachkv(keyvar, variable, container) \
for (QForeachContainer<__typeof__(container)> _container_(container); \
!_container_.brk && _container_.i != _container_.e; \
__extension__ ({ ++_container_.brk; ++_container_.i; })) \
for (keyvar = _container_.i.key();; __extension__ ({break;})) \
for (variable = *_container_.i;; __extension__ ({--_container_.brk; break;}))
Now you can do the following:
QVariantHash m;
m.insert("test", 42);
m.insert("foo", true);
foreachkv(QString k, QVariant v, m)
qDebug() << k << "=>" << v;
The output will be:
"foo" => QVariant(bool, true) "test" => QVariant(int, 42)
Tests I've done so far:
- examples from above
foreachkv
works as expected withbreak
andcontinue
statements within the loop.
What did I do? I just added another for loop to introduce the key variable (called keyvar
in the macro). Maybe I don't need this loop?