Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. converting QVariant to QList
Forum Updated to NodeBB v4.3 + New Features

converting QVariant to QList

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 859 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mzimmers
    wrote on 2 May 2023, 16:52 last edited by mzimmers 5 Feb 2023, 16:55
    #1

    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?

    C J 2 Replies Last reply 2 May 2023, 17:38
    0
    • M mzimmers
      2 May 2023, 16:52

      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?

      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 2 May 2023, 17:38 last edited by Chris Kawa 5 Feb 2023, 17:39
      #2

      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.

      M 1 Reply Last reply 2 May 2023, 18:08
      4
      • C Chris Kawa
        2 May 2023, 17:38

        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.

        M Offline
        M Offline
        mzimmers
        wrote on 2 May 2023, 18:08 last edited by
        #3

        @Chris-Kawa that approach gives me a list of size 0. Do I need to write a converter for my list class?

        1 Reply Last reply
        0
        • M mzimmers
          2 May 2023, 16:52

          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?

          J Online
          J Online
          JonB
          wrote on 2 May 2023, 18:11 last edited by JonB 5 Feb 2023, 18:12
          #4

          @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 does qv.toList() give you? And if that does give you something, what is inside the QVariant's in the list?

          M 1 Reply Last reply 2 May 2023, 18:16
          2
          • J JonB
            2 May 2023, 18:11

            @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 does qv.toList() give you? And if that does give you something, what is inside the QVariant's in the list?

            M Offline
            M Offline
            mzimmers
            wrote on 2 May 2023, 18:16 last edited by
            #5

            @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...

            1 Reply Last reply
            0
            • M mzimmers has marked this topic as solved on 3 May 2023, 21:48

            2/5

            2 May 2023, 17:38

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved