Solved QVariant to QList
-
@mzimmers said in QVariant to QList:
QList<QString> qls = static_cast<QList<QString>>(value.toList());
I am not sure static_cast works. Can you try to convert one after another with toString() in the list?
-
@mzimmers
I don't think a list of variants is/can be the same thing as a list of strings, or converted between the two/treated as such? (AQVariant
cannot be aQString
, it can only contain one.) My thought is you would want to iterate, appending each element?
Note that this is purely a guess as I read this :) -
@JonB I think you're saying basically the same thing that @JoeCFD is, and I think you're both right.
But I still don't know how to cast/convert my QVariant value into a QString (or a QList<QString>).
EDIT: though this comes close:
for (QString qs : value.toStringList()) { item.equipment.append(qs); }
EDIT 2: which means this comes even closer:
case EquipmentRole: item.equipment.clear(); item.equipment.append(value.toStringList());
-
@mzimmers item.equipment.append(value.toStringList()); is a good one. Does it work?
-
@JoeCFD I can't tell for sure until I can expose this in QML, but I'm fairly sure it's working. I'll ask my other question in the QML forum.
Thanks for the help, guys.
EDIT: yeah, it seems to have worked.
-
got this from ChatGPT and you are good.
if (variant.canConvert<QStringList>()) {
QList<QString> stringList = variant.toStringList();
} -
@JoeCFD yeah, I saw the canConvert() method in the docs, but forgot to add it to my code. Thanks for the reminder.
-
-
Understand. Think about you can never be the number 1 or even close anymore if you play chess.
-
@JoeCFD said in QVariant to QList:
Understand. Thiink about you can never be number 1 anymore if you play chess.
Oddly enough, that was never a concern of mine.