Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Passing a List of Urls from QML to C++ using a signal
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 7.1k 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 6 Mar 2014, 08:28 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on 6 Mar 2014, 09:33 last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Korchkidu
        wrote on 6 Mar 2014, 09:53 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on 6 Mar 2014, 10:08 last edited by
          #4

          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)
          }@

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Korchkidu
            wrote on 6 Mar 2014, 10:09 last edited by
            #5

            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!

            1 Reply Last reply
            0
            • O Offline
              O Offline
              onek24
              wrote on 6 Mar 2014, 10:10 last edited by
              #6

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

              1 Reply Last reply
              0
              • L Offline
                L Offline
                livelylama
                wrote on 27 Mar 2014, 15:00 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  onek24
                  wrote on 27 Mar 2014, 16:26 last edited by
                  #8

                  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)@

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    livelylama
                    wrote on 27 Mar 2014, 16:49 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      Xander84
                      wrote on 27 Mar 2014, 19:02 last edited by
                      #10

                      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?

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        livelylama
                        wrote on 27 Mar 2014, 20:22 last edited by
                        #11

                        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 (:

                        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