FileDialog, fileUrls and C++
-
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...
-
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 ?
-
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...
-
@gardiol
fileUrls
returns a list ofQUrl
's. TryQVariantList
QList<QUrl>
on c++ side instead.Edited
-
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.
-
@gardiol How do you send the list from QML to c++ ? Can you post it ?
-
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.
-
@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 asvoid myclass::importFile(QList<QUrl> filenames) { foreach ( QUrl tmp, filenames ) { qDebug() << tmp.toLocalFile(); } }
-
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) -
... 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
returnsQUrl
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.