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. FileDialog, fileUrls and C++
Forum Updated to NodeBB v4.3 + New Features

FileDialog, fileUrls and C++

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 3.1k Views 2 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.
  • G Offline
    G Offline
    gardiol
    wrote on last edited by p3c0
    #1

    Hi!
    i have a FileDialog in QML which calls a C++ slot when the user selectes multiple files. I am banging my head since yesterday but i cannot get the filesnames in C++.

    My slot is defined like this:

        void myclass::import(QVariant filenames)
        {
            QList<QString> copy = filenames.toList();
            foreach ( QString tmp, copy )
            {
                qDebug() <<  tmp;
            }
        }
    

    And in no way i managed to correctly read that list of selected filenames!!! I CAN see the correct filenames in the debugger when i am inside the C++ method... but how can i actually access them?

    I tried everything, casting, calling QVariant::convert(), ANYTHING.

    Possible that there is NOTHING on the documentation on this?

    thank you for any help...

    p3c0P 1 Reply Last reply
    0
    • G gardiol

      Hi!
      i have a FileDialog in QML which calls a C++ slot when the user selectes multiple files. I am banging my head since yesterday but i cannot get the filesnames in C++.

      My slot is defined like this:

          void myclass::import(QVariant filenames)
          {
              QList<QString> copy = filenames.toList();
              foreach ( QString tmp, copy )
              {
                  qDebug() <<  tmp;
              }
          }
      

      And in no way i managed to correctly read that list of selected filenames!!! I CAN see the correct filenames in the debugger when i am inside the C++ method... but how can i actually access them?

      I tried everything, casting, calling QVariant::convert(), ANYTHING.

      Possible that there is NOTHING on the documentation on this?

      thank you for any help...

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @gardiol

      I CAN see the correct filenames in the debugger when i am inside the C++ method... but how can i actually access them?

      Can you explain what do you mean by this ?

      157

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gardiol
        wrote on last edited by
        #3

        If i put a breakpoint in the C++ method, then i see the "filenames" parameter in the debugger window. If i open it, i can see a "data" member containing a list of what looks to be strings, which matches the filenames i selected in the FileDialog, on QML side...

        p3c0P 1 Reply Last reply
        0
        • G gardiol

          If i put a breakpoint in the C++ method, then i see the "filenames" parameter in the debugger window. If i open it, i can see a "data" member containing a list of what looks to be strings, which matches the filenames i selected in the FileDialog, on QML side...

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by p3c0
          #4

          @gardiol
          fileUrls returns a list of QUrl's. Try QVariantList QList<QUrl> on c++ side instead.

          Edited

          157

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gardiol
            wrote on last edited by
            #5

            No, it does not help. What i get is a QVariant, not a QVariantList. Specifically, it's a QVariant with a "user" type. If i try to convert to QVariantList or to call toList(), i just get an empty QVariant and los what's inside.

            p3c0P 1 Reply Last reply
            0
            • G gardiol

              No, it does not help. What i get is a QVariant, not a QVariantList. Specifically, it's a QVariant with a "user" type. If i try to convert to QVariantList or to call toList(), i just get an empty QVariant and los what's inside.

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @gardiol How do you send the list from QML to c++ ? Can you post it ?

              157

              1 Reply Last reply
              0
              • G Offline
                G Offline
                gardiol
                wrote on last edited by p3c0
                #7

                This is my QML (relevant) code:

                    FileDialog {
                        id: loadTrack
                        title: "Load a file"
                        selectMultiple: true
                        onAccepted: {
                            myclass_instance.importFiles( fileUrls );
                        }
                    }
                

                This is my C++ code (well, one of the various iterations, the one which gives me the list in the debugger window):

                    void myclass::importFile(QVariant filenames)
                    {
                        QList<QString> copy = filenames.toList();
                        foreach ( QString tmp, copy )
                        {
                            qDebug() <<  tmp;
                        }
                    }
                

                The code itself works, the method C++ method is called. There is more code of course (like registering the myclass instance to QML and opening the file dialog) but i guess it's not relevant here.

                p3c0P 1 Reply Last reply
                0
                • G gardiol

                  This is my QML (relevant) code:

                      FileDialog {
                          id: loadTrack
                          title: "Load a file"
                          selectMultiple: true
                          onAccepted: {
                              myclass_instance.importFiles( fileUrls );
                          }
                      }
                  

                  This is my C++ code (well, one of the various iterations, the one which gives me the list in the debugger window):

                      void myclass::importFile(QVariant filenames)
                      {
                          QList<QString> copy = filenames.toList();
                          foreach ( QString tmp, copy )
                          {
                              qDebug() <<  tmp;
                          }
                      }
                  

                  The code itself works, the method C++ method is called. There is more code of course (like registering the myclass instance to QML and opening the file dialog) but i guess it's not relevant here.

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @gardiol
                  First please use ``` (3 backticks) for posting code blocks. Otherwise it is boring to read the code.

                  You should use QList<QUrl> on C++ side. Modify the function as

                  void myclass::importFile(QList<QUrl> filenames) {
                        foreach ( QUrl tmp, filenames ) {
                              qDebug() <<  tmp.toLocalFile();
                        }
                  }
                  

                  157

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gardiol
                    wrote on last edited by gardiol
                    #9

                    Yes! a QList<QUrl> works!

                    A little bit of more details in the documentation would help i guess. I didn't get to think it as a list QUrls... probably my fault, but at least some more warnings from the compiler or the MOC would have helped.

                    Thanks a lot!!

                    What baffles me is that a QList<QString> would NOT cast properly and result in an ampty list.
                    (also thanks for the three backticks thing, i could not find how to do it)

                    p3c0P 1 Reply Last reply
                    0
                    • G gardiol

                      Yes! a QList<QUrl> works!

                      A little bit of more details in the documentation would help i guess. I didn't get to think it as a list QUrls... probably my fault, but at least some more warnings from the compiler or the MOC would have helped.

                      Thanks a lot!!

                      What baffles me is that a QList<QString> would NOT cast properly and result in an ampty list.
                      (also thanks for the three backticks thing, i could not find how to do it)

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by p3c0
                      #10

                      @gardiol

                      ... but at least some more warnings from the compiler or the MOC would have helped.

                      This wont work in case of QML, it doesn't get compiled but just interpreted at runtime like javascript.

                      What baffles me is that a QList<QString> would NOT cast properly and result in an ampty list.

                      This is because fileUrls returns QUrl which is completely different type than QString and hence it wont cast.

                      P.S The new forum uses MarkDown language syntax rules. Here's a link for others syntax’s if required.

                      157

                      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