QVariant to QList
-
wrote on 9 Jan 2023, 23:26 last edited by
It seems that value.toList() returns QList<QVariant>, not QList<QString>. You may need explicit conversion.
-
wrote on 9 Jan 2023, 23:32 last edited by
This doesn't work either:
QList<QString> qls = static_cast<QList<QString>>(value.toList());
I think I'm overlooking something fairly basic here.
-
This doesn't work either:
QList<QString> qls = static_cast<QList<QString>>(value.toList());
I think I'm overlooking something fairly basic here.
wrote on 9 Jan 2023, 23:40 last edited by@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?
-
This doesn't work either:
QList<QString> qls = static_cast<QList<QString>>(value.toList());
I think I'm overlooking something fairly basic here.
wrote on 9 Jan 2023, 23:46 last edited by JonB 1 Sept 2023, 23:48@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 :) -
@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 :)wrote on 9 Jan 2023, 23:49 last edited by mzimmers 1 Oct 2023, 00:54@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());
-
@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());
-
wrote on 10 Jan 2023, 00:06 last edited by mzimmers 1 Oct 2023, 00:14
@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.
-
@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();
} -
got this from ChatGPT and you are good.
if (variant.canConvert<QStringList>()) {
QList<QString> stringList = variant.toStringList();
}wrote on 10 Jan 2023, 12:40 last edited by -
wrote on 10 Jan 2023, 14:57 last edited by JoeCFD 1 Oct 2023, 15:31
Understand. Think about you can never be the number 1 or even close anymore if you play chess.
-
Understand. Think about you can never be the number 1 or even close anymore if you play chess.
wrote on 10 Jan 2023, 14:59 last edited by@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.
11/13