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. [solved] std::vector to QList?
Forum Updated to NodeBB v4.3 + New Features

[solved] std::vector to QList?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 18.0k Views 1 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.
  • T Offline
    T Offline
    ThaRez
    wrote on 24 Apr 2012, 10:35 last edited by
    #1

    hello
    I would like to convert a std vector into a QList. Thing is, the type might vary, so I'd like to use a QList<QVariant> (the std vector might be int, double or string but I know this when I add the data).
    I tried the following

    @QList<QVariant> tmp = QList<int>::fromVector( QVector<int>::fromStdVector(stdVectorInt)); @

    But is fails as the int type doesn't match the qvariant type. Is ther enay way of doing this without having to loop trough the vector and add one item at a time? Thanks
    Richard

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lycis
      wrote on 24 Apr 2012, 10:52 last edited by
      #2

      I don't think there's a way for this automatic conversion, because there is AFAIK no way to set

      @QList<X> = QList<Y>@

      because these are different datatypes and the compiler does not know how to convert them.

      You could probably subclass QList to do that conversion, but I think this would require looping through the list - but on the other hand I think that's what is done anyway.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        miroslav
        wrote on 24 Apr 2012, 17:40 last edited by
        #3

        You can use std::copy in combination with a std::back_inserter to achieve that in a one-liner. This is effectively the same any "fromStdList" method would do.

        Mirko Boehm | mirko@kde.org | KDE e.V.
        FSFE Fellow
        Qt Certified Specialist

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThaRez
          wrote on 25 Apr 2012, 12:14 last edited by
          #4

          Thank you for your replies. A related question, can I store a QVariantList inside another QVariantList? Example

          a contains 3 QVariants
          b contains 4 QVariants

          so that:

          c contains a & b -> c.size() == 2
          Thanks!
          Richard

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThaRez
            wrote on 7 May 2012, 08:16 last edited by
            #5

            Found the solution. The problem was I was using the << operator which apparently just added the individual QVariants of the list, not the list itself. Got it working by using .append instead.

            • Richard
            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