Some time ago, I wrote a post about integrating Qt’s associative containers with the fancy new C++ features, range-based for loops with structured bindings.

That post inspired KDAB’s own Giuseppe D’Angelo to add the asKeyValueRange member function to both QHash and QMap. Now it’s possible to iterate over them with a simple range-based for loop, like so:

for (auto [key, value] : map.asKeyValueRange()) {
    // ...

The second part of my previous post demonstrates how we can iterate over Qt SQL results using a range-based for loop as if it were an ordinary collection.

This post covers what is needed to be able to traverse the SQL results in a much nicer and safer way like so:

for (auto [active, name, team] : QSqlResultTypedRange<bool, QString, QString>(query)) {
    qDebug() << "active(bool):" << active;
    qDebug() << "name(QString):" << name;
    qDebug() << "team(QString):" << team;