[Interest] Set manipulation in Qt 6
André Pönitz
apoenitz at t-online.de
Sat Jun 20 10:38:49 CEST 2020
On Sat, Jun 20, 2020 at 08:44:19AM +0200, Vadim Peretokin wrote:
> Being good users open-source of Qt, [1]we're looking into our
> compatibility early on to help report any issues and we're finding this
> compatibility code to be rather problematic from the developer
> experience point of view:
> QMap<QString, QList<QPointF>> customLines;
> QMap<QString, QColor> customLinesColor;
> // ...
> #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
> auto customLineKeys = customLines.keys();
> QSet<QString> missingKeys{customLineKeys.begin(),
> customLineKeys.end()};
> if (!customLinesColor.isEmpty()) {
> auto customLinesColorKeys = customLinesColor.keys();
> QSet<QString>
> customLinesColorKeysSet{customLinesColorKeys.begin(),
> customLinesColorKeys.end()};
> missingKeys.subtract(customLinesColorKeysSet);
> }
> #else
> QSet<QString>
> missingKeys{customLines.keys().toSet().subtract(customLinesColor.keys()
> .toSet())};
> #endif
> Are we doing it wrong or is this a regression in Qt? ([2]QTBUG-83697)
I don't think you are doing anything wrong here.
The Qt Creator code base currently has the following in a central header:
// Replacement for deprecated Qt functionality
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
template <class T>
QSet<T> toSet(const QList<T> &list)
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
return list.toSet();
#else
return QSet<T>(list.begin(), list.end());
#endif
}
#endif
template<class T>
QSet<T> toSet(const QVector<T> &vec)
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
QSet<T> result;
for (const T &p : vec) {
result.insert(p);
}
return result;
#else
return QSet<T>(vec.begin(), vec.end());
#endif
}
template<class T>
QList<T> toList(const QSet<T> &set)
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
return set.toList();
#else
return QList<T>(set.begin(), set.end());
#endif
}
template <class Key, class T>
void addToHash(QHash<Key, T> *result, const QHash<Key, T> &additionalContents)
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
result->unite(additionalContents);
#else
result->insert(additionalContents);
#endif
}
and used those functions to replace the previously used member functions.
This is not really nice either, but at least keeps the main application code
free of preprocessor #if's.
Andre'
Andre'
More information about the Interest
mailing list