[SOLVED] From QVariant to QList<QUrl>



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


  • Moderators

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



  • Excellent! It works just fine!

    Thanks!



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.