PDA

View Full Version : QList<double> => QVariant => QList<double>



stefan
12th October 2013, 11:00
Hi!

I'm trying to store my QList to QVariant and than convert it back to QList.

This is minimal example of what I'm trying to do. Real application would be more complex, but on same basis.



#include <QCoreApplication>
#include <QVariant>
#include <QList>
#include <iostream>

Q_DECLARE_METATYPE(QList<double>)

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

qRegisterMetaType<QList<double> >("QList<double>");

QList<double> x1;
for(int i=0; i<30; i++)
x1.append((double)i);

QVariant x2;
x2.fromValue<QList<double> >(x1);

QList<double> x3 = x2.value<QList<double> >();
for(int i=0; i<x3.size(); i++)
std::cout << x3[i] << "\n";

std::cout << "***";

return a.exec();
}


Above code compiles without any warnings but running the code does not produce expected results.

Can somebody tell me what am I doing wrong?
Thanx!

Santosh Reddy
12th October 2013, 11:15
Does this look simpler


QList<QVariant> x1;
for(int i=0; i<30; i++)
x1.append((double)i);

QVariant x2 = x1;

QList<QVariant> x3 = x2.toList();
for(int i=0; i<x3.size(); i++)
std::cout << x3.at(i).toDouble() << "\n";

stefan
12th October 2013, 11:20
Yes, this could solve my issue!
Thank you!

anda_skoa
12th October 2013, 12:17
You problem is here:





QVariant x2;
x2.fromValue<QList<double> >(x1);



QVariant::fromValue() returns a QVariant. It is a static method, it does not (cannot) change x2.



QVariant x2 = QVariant::fromValue(x1);


Cheers,
_