Solved When is it appropriate to use "file://" for a QUrl?
-
We have an object that uses this object to provide SaveAs dialogs for QML:
class FileSaveDialog : public QQuickItem
Inside the object we define 2 more objects to facilitate this:
QPlatformFileDialogHelper *m_dlgHelper; ... QSharedPointer<QFileDialogOptions> m_options;
At some point I get a path:
C:\\ProgramData\\XM Editor\\RnD\\programs
I set that path on m_options:
m_options->setInitialDirectory(folderUrl_);
QML is involved in this chain of events. It sets the path of the folderUrl property on our object.
Problems:
- If I set this path on the object, which is a QUrl (folderUrl_) then it opens up the dialog to an appropriate place. It however throws a message in the console:
shellItem : Unhandled scheme: "c"
- If I append "file://" to the beginning it grinds away and puts a network access error in the console and then finally shows the dialog and the correct path:
shellItem: SHCreateItemFromParsingName()) failed (The network path was not found.)
*if I append "file:///" to the beginning I don't see any messages in the console and it appears to open to the correct directory.
So, it seems like I should be using "file://", but by doing so I have to prepend an extra "/" to the directory for Windows systems only. I believe under Linux our directories are absolute so already have a "/". This makes it so I have to have conditional code for Windows systems:
#ifdef Q_OS_WIN emit saveLocalFile("/" + cell_info->progDir(), filename); #else emit saveLocalFile(cell_info->progDir(), filename); #endif
This seems like it should not require this conditional code. I have tried changing the directory to:
QDir(cell_info->progDir()).canonicalPath() ... C:/ProgramData/XM Editor/RnD/programs
This gives the same output and results for all 3 cases as with the output and results of the escaped windows string.
-
@fcarney I recently stumbled over the same, and yes, Windows requires 3 slashes after file:
Have you tried
QUrl::fromLocalFile
?
I guess it should handle it correctly. -
@fcarney , @aha_1980
EDIT I think I was indeed wrong!**I could well be wrong on this :) But I thought I have come across cases where you're not supposed to put a:
after the drive letter when inside afile:...
protocol? So it should be more like:~~ file:///C/ProgramData/XM Editor/RnD/programs ~~
(Not sure about the
///
vs//
.) Take my comment with a pinch of salt, throw it over your shoulder if it's nonsense.... -
(Not sure about the /// vs //.) Take my comment with a pinch of salt, throw it over your shoulder if it's nonsense....
Done.
PS: QUrl::fromLocalFile() really works :)
-
@aha_1980 said in When is it appropriate to use "file://" for a QUrl?:
Have you tried QUrl::fromLocalFile?
Wow, I used this as a filter:
QUrl().fromLocalFile(cell_info->progDir()).toString()
It produced:
file:///C:/ProgramData/XM Editor/RnD/programs
So, yes, just tried it. That should be how I handle url local paths from now on. What a great find, thank you.
-
Just in case, QUrl::fromLocalFile is a static method so no need to instantiate a QUrl object to use it.