Convert QVariantList to QList<Type> list
General and Desktop
23
Posts
4
Posters
28.8k
Views
3
Watching
-
Hi,
I'm not a C++ super-guru but I don't think you can do it because the compiler must know the type of each variable at compile time.
A solution could be implement
qHash(const QVariant &v)
and useQSet<QVariant>
-
-
You can also implement qHash for the type you need to support and thus it will be used automatically in all your software.
#include <QtDebug> inline uint qHash(const QVariant &key, uint seed = 0) { switch (key.userType()) { case QVariant::Int: return qHash(key.toInt(), seed); case QVariant::UInt: return qHash(key.toUInt(), seed); // add all cases you want to support; } return 0; } int main( int argc, char * argv[] ) { QApplication app( argc, argv ); QVariantList vl1; QVariantList vl2; for (int i = 0 ; i < 5 ; ++i) { vl1 << i; } for (int i = 0 ; i < 3 ; ++i) { vl2 << i; } qDebug() << (vl1.toSet() & vl2.toSet()); return 0; }