PDA

View Full Version : Casting QStringList to QList<QVariant>



Valheru
5th October 2006, 20:51
Can I simply cast this using a static/dynamic/const cast? If so, what is the syntax for it?

jpn
5th October 2006, 20:57
QStringList is a QList<QString>. But it is not possible cast it to QList<QVariant>. That's two template containers containing different types.

Edit: BTW, see qCopy (http://doc.trolltech.com/4.1/qtalgorithms.html#qCopy). Maybe it suits for your need:


QStringList stringList;
stringList << "one" << "two" << "three";

QVector<QVariant> vector(strings.size());
qCopy(stringList.begin(), stringList.end(), vector.begin()); // works because QVariant has
// a constructor taking QString

QList<QVariant> variantList = vector.toList(); // if you really need a list

wysota
5th October 2006, 21:17
The only thing you can do is:


QList<QVariant> variantlist;
QStringList slist;
foreach(QString s, slist){
variantlist << s;
}

Valheru
5th October 2006, 22:24
Thanks. You two are most helpfull :) Don't you ever get tired of teh n00bs? ;)