[SOLVED] Passing a List of Urls from QML to C++ using a signal



  • Hi,

    I have a "FileDialog":http://qt-project.org/doc/qt-5/qml-qtquick-dialogs-filedialog.html from which I get a "list<url> (fileUrls)":http://qt-project.org/doc/qt-5/qml-qtquick-dialogs-filedialog.html#fileUrls-prop.

    I want to pass this "list<url> (fileUrls)":http://qt-project.org/doc/qt-5/qml-qtquick-dialogs-filedialog.html#fileUrls-prop as a signal parameter and connect it to a C++ slot.

    The following QML does not work:

    @
    signal filesOpened(list<url> fileUrls)

    FileDialog
    {
    id: openFileDialog
    title: "Please choose a file"
    selectMultiple: true

    onAccepted: filesOpened(fileUrls)
    onRejected: visible = false
    

    }

    @

    The error I get is:
    @main.qml:2 Unexpected token `<'@

    So what is the correct way to pass a list<url> as a signal parameter?

    Thanks for any help.



  • Hey,

    This documentations might help you out:
    "Data Type Conversion Between QML and C++":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-data.html
    "QDeclarativeModels":http://qt-project.org/doc/qt-4.8/qdeclarativemodels.html



  • Thanks for the links. I read it and it seems to me that I should pass a variant to my signal. Am I on the right track?



  • You're welcome. You can pass a variant as an array to Cpp, see:
    "Variant":http://qt-project.org/doc/qt-4.8/qml-variant.html

    But it looks like you can't set a list as a parameter for a signal. It may work if you set a var and then still pass the list to the signal on emit. Example:

    @Rectangle{
    property list<Rectangle> rectlist: [Rectangle{}]
    signal emitMe(var)
    onWidthChanged: emitMe(rectlist)
    }@



  • Thanks a lot!
    This is actually what I did: I pass a variant, then get the QVariant in the C++ code, and convert it back to a QList<QUrl> and it works.

    Thanks a lot for your help!



  • I'm glad that i could help you. :)



  • Hi,
    I ran into exactly same problem. I also have to pass the list<url> (fileUrls) from the FileDialog to C++. I did everything as you described above:

    I define the signal using: @signal sendUrls(var variant)@

    I send the signal using: @sendUrls(fileDialog.fileUrls)@

    But in the C++ I only get an empty variant. If I pass the recieved variant into qDebug I get "QVariant(QList<QUrl>, )" as output.
    So I dont actually recieve anything useful from the signal.
    If I instead make the signal to transmit an integer and I send sendUrls(fileDialog.fileUrls.length) i recieve the integer and everything works fine.

    So maybe you could tell me how exactly you managed to send the list<url> as a variant and how you then converted it to QList<QUrl>. Or maybe just post her that part of your code which deals with sending and receiving the the list<url> / Qvariant

    I would be very grateful for any help.



  • Hello and welcome to devnet,

    var and variant are both types, so i would say there is the problem. It's like you were trying to declare an int char = 0 in Cpp. Just write:
    @signal sendUrls(var)@
    or
    @signal sendUrls(variant)@



  • Hi thanks for your reply.
    I changed the line now to @signal sendUrls(variant name)@

    this doesn't solve my problem though. I still don't get a proper QVariant into my cpp code. I still get QVariant(QList<QUrl>, ) as output when I call @recieveUrllist(QVariant Urllist)
    {
    qDebug() << Urllist;
    }
    @

    also I can't declare a signal without writing a name, so what you proposed to me
    @signal sendUrls(variant)@

    gives me an error: Expected token `identifier'

    When i use
    @signal sendUrls(variant name)@

    or
    @signal sendUrls(var name)@

    i can compile it. The result stays the same whether i use var or variant.

    So basically I want to pass my Urllist which I get from a filedialog into a c++ method so that I can use it there. If you know a way thats better then sending a variant via a signal i would be happy to hear it as well.

    Thanks for your help so far,
    It is much appreciated.



  • just a question (and maybe solution), why do you need to define a signal in QML for this? can't you simply call the c++ slot and pass the URL list directly?



  • Yes that works. I thought I had to use a signal because it was mentioned here in this forum. But it is much easier just to pass on the URLList right into a function as you say. It works very fine now.

    With your reply of two lines you saved me a couple of hours of trail and error. Many thanks for that (:


Log in to reply
 

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