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 Updated to NodeBB v4.3 + New Features

Convert QVariantList to QList<Type> list

Scheduled Pinned Locked Moved General and Desktop
qvariantlist ql
23 Posts 4 Posters 28.8k 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 mcosta

    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 last edited by
    #21

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

    1 Reply Last reply
    0
    • I ikim

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

      MayEnjoyM Offline
      MayEnjoyM Offline
      MayEnjoy
      wrote on 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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

        • Login

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