Let's consider this example:
QVariant v1(1);
QVariant v2("goofy");
QVariantList list;
list << v1 << v2;
for (const auto& var : list) {
qdebug() << var;
// nasty part
if (var.type == QVariant::Int) {
int value = var.toInt();
// do something
} else if (var.type == QVariant::QString) {
QString value = var.toString();
// do something
}
}
The debug function shows the internal storage type of QVariant:
QVariant(int, 1) QVariant(QString, "goofy")
Is there a way to avoid the if
s and do an explicit cast in order to access the internal type? More specifically, to get the value I would like to be able to write something like this:
auto value = var.ToData();
Edit: Since QVariant can hold a lot of type and you can even add custom types on it, it would be enough restricting the problem only to base types (int, double, bool, string)