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. Convert QVariantList to QList<Type> list
Forum Update on Monday, May 27th 2025

Convert QVariantList to QList<Type> list

Scheduled Pinned Locked Moved General and Desktop
qvariantlist ql
23 Posts 4 Posters 26.7k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 26 Mar 2015, 22:37 last edited by
    #8

    QVariant has the == operator

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    I 1 Reply Last reply 26 Mar 2015, 22:43
    0
    • S SGaist
      26 Mar 2015, 22:37

      QVariant has the == operator

      I Offline
      I Offline
      ikim
      wrote on 26 Mar 2015, 22:43 last edited by
      #9

      @SGaist yes I could compare each QVariant in the list but that would mean iterating over the entire list to determine if the item exists in the second list. Is that my only option ?

      Thanks

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 26 Mar 2015, 22:49 last edited by
        #10

        No, use the QSet features e.g.

        QSet<QVariant> commonSet = mySet1 & mySet2;
        

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        I 1 Reply Last reply 26 Mar 2015, 23:04
        0
        • S SGaist
          26 Mar 2015, 22:49

          No, use the QSet features e.g.

          QSet<QVariant> commonSet = mySet1 & mySet2;
          
          I Offline
          I Offline
          ikim
          wrote on 26 Mar 2015, 23:04 last edited by
          #11

          @SGaist Thanks, so I can use a QSet<QVariant> out of the box ?

          other forums are suggesting that I need to define a qhash() function for the QVariant.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on 26 Mar 2015, 23:58 last edited by
            #12

            QSet is based on a hash table; this means that internally it uses qHash()

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            I 1 Reply Last reply 27 Mar 2015, 08:34
            0
            • I ikim
              26 Mar 2015, 22:02

              Hi All

              I have a QVariantList which is passed as a parameter to my function.

              void myfunc(const QVariantList &list)
              {
                // I want to convert this to  QList<Type> newList
                // where Type is the type() of list.at(0)
                // the variant list consists of variants all of the same type 
              }
              

              What I want to do is

              QList<Type> newList;   //how do I declare this if I dont know the Type
                foreach(QVariant v, list) 
                  newList << v.value();
              

              Then create a QSet from the newList

              Is there a way of easily achieving this

              Regards

              Ikim

              M Offline
              M Offline
              MayEnjoy
              wrote on 27 Mar 2015, 05:48 last edited by
              #13

              @ikim why not use QVariant::Type method? even you can use it with c++ template.

              I am a engineer.

              I 1 Reply Last reply 27 Mar 2015, 08:40
              0
              • M mcosta
                26 Mar 2015, 23:58

                QSet is based on a hash table; this means that internally it uses qHash()

                I Offline
                I Offline
                ikim
                wrote on 27 Mar 2015, 08:34 last edited by
                #14

                @mcosta Thanks but this gives a compile error

                QSet<QVariant> existingValues;
                existingValues.insert(1);

                1 Reply Last reply
                0
                • M MayEnjoy
                  27 Mar 2015, 05:48

                  @ikim why not use QVariant::Type method? even you can use it with c++ template.

                  I Offline
                  I Offline
                  ikim
                  wrote on 27 Mar 2015, 08:40 last edited by
                  #15

                  @MayEnjoy Thanks, but how would this enable me to achieve my goal ?

                  M 1 Reply Last reply 28 Mar 2015, 07:15
                  0
                  • M Offline
                    M Offline
                    mcosta
                    wrote on 27 Mar 2015, 08:50 last edited by
                    #16

                    Which kind of error?

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    I 1 Reply Last reply 27 Mar 2015, 08:55
                    0
                    • M mcosta
                      27 Mar 2015, 08:50

                      Which kind of error?

                      I Offline
                      I Offline
                      ikim
                      wrote on 27 Mar 2015, 08:55 last edited by
                      #17

                      @mcosta /usr/include/QtCore/qhash.h:882:24: error: no matching function for call to 'qHash(const QVariant&)'

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mcosta
                        wrote on 27 Mar 2015, 09:11 last edited by
                        #18

                        mmmm, You're right.

                        So I suggest to create a template method

                        This code works for me

                        template <typename T>
                        QSet<T> mergeList(const QVariantList& l1, const QVariantList& l2) {
                            QSet<T> s1, s2;
                        
                            for (const QVariant& v: l1) {
                                s1.insert(v.value<T>());
                            }
                        
                            for (const QVariant& v: l2) {
                                s2.insert(v.value<T>());
                            }
                        
                            return s1 & s2;
                        }
                        

                        Used as

                            QVariantList l1, l2;
                            l1 << 1 << 2 << 3;
                            l2 << 1 << 3 << 5;
                        
                            QSet<int> s1 = mergeList<int> (l1, l2);
                            qDebug() << s1;
                        
                            l1.clear();
                            l2.clear();
                        
                            l1 << "Foo" << "Bar";
                            l2 << "Bar" << "Fred";
                        
                            QSet<QString> s2 = mergeList<QString> (l1, l2);
                            qDebug() << s2;
                        

                        Once your problem is solved don't forget to:

                        • Mark the thread as SOLVED using the Topic Tool menu
                        • Vote up the answer(s) that helped you to solve the issue

                        You can embed images using (http://imgur.com/) or (http://postimage.org/)

                        I 1 Reply Last reply 27 Mar 2015, 09:30
                        1
                        • M mcosta
                          27 Mar 2015, 09:11

                          mmmm, You're right.

                          So I suggest to create a template method

                          This code works for me

                          template <typename T>
                          QSet<T> mergeList(const QVariantList& l1, const QVariantList& l2) {
                              QSet<T> s1, s2;
                          
                              for (const QVariant& v: l1) {
                                  s1.insert(v.value<T>());
                              }
                          
                              for (const QVariant& v: l2) {
                                  s2.insert(v.value<T>());
                              }
                          
                              return s1 & s2;
                          }
                          

                          Used as

                              QVariantList l1, l2;
                              l1 << 1 << 2 << 3;
                              l2 << 1 << 3 << 5;
                          
                              QSet<int> s1 = mergeList<int> (l1, l2);
                              qDebug() << s1;
                          
                              l1.clear();
                              l2.clear();
                          
                              l1 << "Foo" << "Bar";
                              l2 << "Bar" << "Fred";
                          
                              QSet<QString> s2 = mergeList<QString> (l1, l2);
                              qDebug() << s2;
                          
                          I Offline
                          I Offline
                          ikim
                          wrote on 27 Mar 2015, 09:30 last edited by
                          #19

                          @mcosta Thanks for your effort, but this still doesn't solve the problem as I dont know the Type at compile time

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            mcosta
                            wrote on 27 Mar 2015, 10:46 last edited by
                            #20

                            Hi,

                            I'm not a C++ super-guru but I don't think you can do it because the compiler must know the type of each variable at compile time.

                            A solution could be implement qHash(const QVariant &v) and use QSet<QVariant>

                            Once your problem is solved don't forget to:

                            • Mark the thread as SOLVED using the Topic Tool menu
                            • Vote up the answer(s) that helped you to solve the issue

                            You can embed images using (http://imgur.com/) or (http://postimage.org/)

                            I 1 Reply Last reply 27 Mar 2015, 10:49
                            1
                            • M mcosta
                              27 Mar 2015, 10:46

                              Hi,

                              I'm not a C++ super-guru but I don't think you can do it because the compiler must know the type of each variable at compile time.

                              A solution could be implement qHash(const QVariant &v) and use QSet<QVariant>

                              I Offline
                              I Offline
                              ikim
                              wrote on 27 Mar 2015, 10:49 last edited by
                              #21

                              @mcosta thanks for your time, I think you may be right :)

                              1 Reply Last reply
                              0
                              • I ikim
                                27 Mar 2015, 08:40

                                @MayEnjoy Thanks, but how would this enable me to achieve my goal ?

                                M Offline
                                M Offline
                                MayEnjoy
                                wrote on 28 Mar 2015, 07:15 last edited by
                                #22

                                @ikim said:

                                @SGaist yes I could compare each QVariant in the list but that would mean iterating over the entire list to determine if the item exists in the second list. Is that my only option ?

                                Thanks

                                I think @mclark 's answer is the better one.

                                I am a engineer.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 28 Mar 2015, 21:32 last edited by
                                  #23

                                  You can also implement qHash for the type you need to support and thus it will be used automatically in all your software.

                                  #include <QtDebug>
                                  
                                  inline uint qHash(const QVariant &key, uint seed = 0)
                                  {
                                      switch (key.userType()) {
                                          case QVariant::Int:
                                              return qHash(key.toInt(), seed);
                                  
                                          case QVariant::UInt:
                                              return qHash(key.toUInt(), seed);
                                  
                                         // add all cases you want to support;
                                  
                                      }
                                  
                                      return 0;
                                  }
                                  
                                  int main( int argc, char * argv[] )
                                  {
                                      QApplication app( argc, argv );
                                      QVariantList vl1;
                                      QVariantList vl2;
                                  
                                      for (int i = 0 ; i < 5 ; ++i) {
                                          vl1 << i;
                                      }
                                  
                                      for (int i = 0 ; i < 3 ; ++i) {
                                          vl2 << i;
                                      }
                                  
                                      qDebug() << (vl1.toSet() & vl2.toSet());
                                  
                                      return 0;
                                  }
                                  

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0

                                  17/23

                                  27 Mar 2015, 08:55

                                  • Login

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