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] From QVariant to QList<QUrl>
Qt 6.11 is out! See what's new in the release blog

[SOLVED] From QVariant to QList<QUrl>

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.5k 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.
  • K Offline
    K Offline
    Korchkidu
    wrote on last edited by
    #1

    Hi,

    I have a QVariant and I need to convert it into a QList<QUrl>. I tried 2 methods:

    Convert the QVariant to QList<QUrl> directly => does NOT work;

    Iterating over the QVariant and reconstruct the QList<QUrl> "manually" => does work.

    CONVERSION
    I tried this:

    @
    // Getting QVariant fileUrlsVariant from "somewhere"
    std::string typeName = fileUrlsVariant.typeName();
    qDebug() << typeName.c_str();
    bool convertToUrls = fileUrlsVariant.canConvert<QList<QUrl>>();
    qDebug() << convertToUrls;
    bool convertToQVariants = fileUrlsVariant.canConvert<QList<QVariant>>();
    qDebug() << convertToQVariants;
    QList<QVariant> variants = fileUrlsVariant.toList();
    qDebug() << variants;
    @

    The output is:
    @QList<QUrl>
    true
    true
    () @

    While it seems i can convert from the QVariant to a QList<QVariant>, doing so results in an empty list. Why that?

    ITERATE + CONSTRUCT
    @// Getting QVariant fileUrlsVariant from "somewhere"
    QSequentialIterable IT = fileUrlsVariant.value<QSequentialIterable>();
    QList<QUrl> fileUrls;
    for (const QVariant &v : IT)
    {
    fileUrls.append(v.toUrl());
    }
    qDebug() << fileUrls;@

    The output is:
    @(QUrl("file:///D:/TEMP/file1.txt") , QUrl( "file:///D:/TEMP/file2.txt" ) , QUrl( "file:///D:/TEMP/file3.txt" ) ) @

    So here, it seems to work. But I would prefer a method converting the QVariant instead of having to iterate it. Any idea?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are using wrong method for getting your list. Try this:
      @
      QList<QUrl> myList = fileUrlsVariant.value<QList<QUrl> >();
      @

      (Z(:^

      F 1 Reply Last reply
      2
      • K Offline
        K Offline
        Korchkidu
        wrote on last edited by
        #3

        Excellent! It works just fine!

        Thanks!

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          You are using wrong method for getting your list. Try this:
          @
          QList<QUrl> myList = fileUrlsVariant.value<QList<QUrl> >();
          @

          F Offline
          F Offline
          frankem
          wrote on last edited by
          #4

          @sierdzio Thanks. You saved my day. :-)

          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