QVariant to QList
-
Hi all -
I'm fooling around with a Model, and am trying to figure out how to correctly code the setter. Here's the relevant code:
struct ActivityItem { QString name; QList<QString> equipment; bool recommended; }; bool ActivityModel::setData(const QModelIndex &index, const QVariant &value, int role) { switch (role) { item.equipment.clear(); item.equipment.append(value.toList()); // error: No matching member function for call to 'append'
The last line isn't right, but the error message isn't very helpful, and the code seems to match the doc (as far as I can tell). Can someone give me an idea of what I'm doing wrong here?
Thanks...
-
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.
@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 :)@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());
-
@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();
} -
Understand. Think about you can never be the number 1 or even close anymore if you play chess.