converting QVariant to QList
-
wrote on 2 May 2023, 16:52 last edited by mzimmers 5 Feb 2023, 16:55
Hi all -
I'd like to convert a QVariant to a QList, like this (if possible):
class EquipmentItem { Q_GADGET ... } typedef QList<EquipmentItem> ZoneEquipmentList; QVariant qv; // does contain something in my real app. ZoneEquipmentList zel = qv.toList();
In reading about this, I found this:
QList<QVariant> QVariant::toList() const Returns the variant as a QVariantList if the variant has userType() QMetaType::QVariantList. If it doesn't, QVariant will attempt to convert the type to a list and then return it. This will succeed for any type that has registered a converter to QVariantList or which was declared as a sequential container using Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE. If none of those conditions are true, this function will return an empty list.QList<QVariant> QVariant::toList() const
I can't find anything on registering a converter. Can someone point me to the correct page for this?
Thanks...
PS: how best to format a passage from one of the Qt docs?
-
Hi all -
I'd like to convert a QVariant to a QList, like this (if possible):
class EquipmentItem { Q_GADGET ... } typedef QList<EquipmentItem> ZoneEquipmentList; QVariant qv; // does contain something in my real app. ZoneEquipmentList zel = qv.toList();
In reading about this, I found this:
QList<QVariant> QVariant::toList() const Returns the variant as a QVariantList if the variant has userType() QMetaType::QVariantList. If it doesn't, QVariant will attempt to convert the type to a list and then return it. This will succeed for any type that has registered a converter to QVariantList or which was declared as a sequential container using Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE. If none of those conditions are true, this function will return an empty list.QList<QVariant> QVariant::toList() const
I can't find anything on registering a converter. Can someone point me to the correct page for this?
Thanks...
PS: how best to format a passage from one of the Qt docs?
Lifetime Qt Championwrote on 2 May 2023, 17:38 last edited by Chris Kawa 5 Feb 2023, 17:39Simply use
ZoneEquipmentList zel = qv.value<ZoneEquipmentList>();
toList
would give you QVariantList, not ZoneEquipmentList, meaning each element would be QVariant that you'd have to convert to your type. It's unnecessary overhead to convert them back and forth. -
Simply use
ZoneEquipmentList zel = qv.value<ZoneEquipmentList>();
toList
would give you QVariantList, not ZoneEquipmentList, meaning each element would be QVariant that you'd have to convert to your type. It's unnecessary overhead to convert them back and forth.wrote on 2 May 2023, 18:08 last edited by@Chris-Kawa that approach gives me a list of size 0. Do I need to write a converter for my list class?
-
Hi all -
I'd like to convert a QVariant to a QList, like this (if possible):
class EquipmentItem { Q_GADGET ... } typedef QList<EquipmentItem> ZoneEquipmentList; QVariant qv; // does contain something in my real app. ZoneEquipmentList zel = qv.toList();
In reading about this, I found this:
QList<QVariant> QVariant::toList() const Returns the variant as a QVariantList if the variant has userType() QMetaType::QVariantList. If it doesn't, QVariant will attempt to convert the type to a list and then return it. This will succeed for any type that has registered a converter to QVariantList or which was declared as a sequential container using Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE. If none of those conditions are true, this function will return an empty list.QList<QVariant> QVariant::toList() const
I can't find anything on registering a converter. Can someone point me to the correct page for this?
Thanks...
PS: how best to format a passage from one of the Qt docs?
wrote on 2 May 2023, 18:11 last edited by JonB 5 Feb 2023, 18:12@mzimmers said in converting QVariant to QList:
QVariant qv; // does contain something in my real app.
Can we start by making 100.0% sure we know what is in there? If
qv.value<ZoneEquipmentList>()
is empty list what doesqv.toList()
give you? And if that does give you something, what is inside theQVariant
's in the list? -
@mzimmers said in converting QVariant to QList:
QVariant qv; // does contain something in my real app.
Can we start by making 100.0% sure we know what is in there? If
qv.value<ZoneEquipmentList>()
is empty list what doesqv.toList()
give you? And if that does give you something, what is inside theQVariant
's in the list?wrote on 2 May 2023, 18:16 last edited by@JonB I just spoke with the author of the back end -- we had a misunderstanding about the contents of the message.
To answer your question, toList() does return a size of 1. But it's merely a UUID, not an actual equipment object as I'd expected.
I think my question is answered, but I'll hold off on closing this topic until I do a bit more experimentation.
Thanks...
-
2/5