QFile doesn't recognize file:/// url path format
Asked Answered
M

4

23

I get the file path from Qml like this:

mainView.projectFilePath = Qt.resolvedUrl(newProjectFileDlg.fileUrl).toString();

The above file path looks like this: file:///C:/uuuu.a3

But when this path is passed to QFile, it complains

The filename, directory name, or volume label syntax is incorrect

How to solve this problem?

Mathis answered 7/1, 2014 at 8:35 Comment(0)
B
17

QString was not meant for a canonical url representation. It is a string class existing mostly due to the utf use cases.

What you are looking for is QUrl which was meant for use cases like this. Pass your path to that, and then get the "QFile-readable" path out of that, then pass it to QFile.

You will need to use the following method for the conversion before passing the path to QFile:

QUrl QUrl::fromLocalFile(const QString & localFile) [static]

Returns a QUrl representation of localFile, interpreted as a local file. This function accepts paths separated by slashes as well as the native separator for this platform.

This function also accepts paths with a doubled leading slash (or backslash) to indicate a remote file, as in "//servername/path/to/file.txt". Note that only certain platforms can actually open this file using QFile::open().

Begotten answered 7/1, 2014 at 12:19 Comment(1)
For me, QUrl( "file:///C:/myfile.txt" ).toLocalFile(); does the conversion to "C:/myfile.txt" while fromLocalFile just seems to prepend "file:///". Also see this answer below.Gusto
E
36
QUrl url(newProjectFileDlg.fileUrl);
url.toLocalFile();

This is probably what you need. It will return "C:/uuuu.a3" in your case.

Ereshkigal answered 17/2, 2014 at 18:51 Comment(1)
This is the correct answer to the question, not the accepted one.Winifredwinikka
B
17

QString was not meant for a canonical url representation. It is a string class existing mostly due to the utf use cases.

What you are looking for is QUrl which was meant for use cases like this. Pass your path to that, and then get the "QFile-readable" path out of that, then pass it to QFile.

You will need to use the following method for the conversion before passing the path to QFile:

QUrl QUrl::fromLocalFile(const QString & localFile) [static]

Returns a QUrl representation of localFile, interpreted as a local file. This function accepts paths separated by slashes as well as the native separator for this platform.

This function also accepts paths with a doubled leading slash (or backslash) to indicate a remote file, as in "//servername/path/to/file.txt". Note that only certain platforms can actually open this file using QFile::open().

Begotten answered 7/1, 2014 at 12:19 Comment(1)
For me, QUrl( "file:///C:/myfile.txt" ).toLocalFile(); does the conversion to "C:/myfile.txt" while fromLocalFile just seems to prepend "file:///". Also see this answer below.Gusto
V
5

How annoying that QML doesn't have this stuff. I have created a helper function which can be called from the QML:

Q_INVOKABLE QString convertUrlToNativeFilePath(const QUrl& urlStylePath) const;

Which simply does this:

QString OurClassName::convertUrlToNativeFilePath(const QUrl& urlStylePath) const
{
    return QDir::toNativeSeparators(urlStylePath.toLocalFile());
}
Vine answered 4/9, 2015 at 4:30 Comment(1)
Thank you, this was helpfulMadson
A
-1

Have a look at QDir::toNativeSeparators(QString)

Anthropophagite answered 11/8, 2015 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.