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...
-
@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());