Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFile doesn't recognize file:/// url path format

Tags:

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?

like image 931
Dean Chen Avatar asked Jan 07 '14 08:01

Dean Chen


People also ask

Can a URL be a file path?

So yes, file URIs are URLs.

How do I load a file in Qt?

abk file to load it into the address book. void AddressBook::loadFromFile() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Address Book"), "", tr("Address Book (*. abk);;All Files (*)")); On Windows, for example, this function pops up a native file dialog, as shown in the following screenshot.

What is QFileInfo?

QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. QFileInfo can also be used to obtain information about a Qt resource.

How do I write a QT file?

To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right: QFile file("out. txt"); if (! file.


2 Answers

QUrl url(newProjectFileDlg.fileUrl);
url.toLocalFile();

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

like image 197
claudiub Avatar answered Sep 20 '22 15:09

claudiub


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().

like image 39
lpapp Avatar answered Sep 21 '22 15:09

lpapp