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

converting QVariant to QList

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 832 Views
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #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?

    Chris KawaC JonBJ 2 Replies Last reply
    0
    • mzimmersM mzimmers

      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?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #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.

      mzimmersM 1 Reply Last reply
      4
      • Chris KawaC Chris Kawa

        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.

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on 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
        • mzimmersM mzimmers

          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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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?

          mzimmersM 1 Reply Last reply
          2
          • JonBJ JonB

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

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on 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
            • mzimmersM mzimmers has marked this topic as solved on

            • Login

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