As noted in the comments already, there seems to be no way (yet?) to get the path itself without a regex. So this is the only way to go:
Basic solution
FileDialog {
onAccepted: {
var path = myFileDialog.fileUrl.toString();
// remove prefixed "file:///"
path = path.replace(/^(file:\/{3})/,"");
// unescape html codes like '%23' for '#'
cleanPath = decodeURIComponent(path);
console.log(cleanPath)
}
}
This regex should be quite robust as it only removes the file:///
from the beginning of the string.
You will also need to unescape some HTML characters (if the file name contains e.g. the hash #
, this would be returned as %23
. We decode this by using the JavaScript function decodeURIComponent()
).
Fully featured example
If you not only want to filter the file:///
but also qrc://
and http://
, you can use this RegEx:
^(file:\/{3})|(qrc:\/{2})|(http:\/{2})
So the new, complete code would be:
FileDialog {
onAccepted: {
var path = myFileDialog.fileUrl.toString();
// remove prefixed "file:///"
path= path.replace(/^(file:\/{3})|(qrc:\/{2})|(http:\/{2})/,"");
// unescape html codes like '%23' for '#'
cleanPath = decodeURIComponent(path);
console.log(cleanPath)
}
}
This is a good playground for RegEx'es: http://regex101.com/r/zC1nD5/1
RFC 3986
URL starts with protocol. In your case it isfile://
.All Qt components understand it, even worse sometimes you will get an error while passing something likec:/bar.txt
. If you still want to get this URL inwrong
way you just want to replace the protocol part -theurl.toString().replace("file:///","")
file:///
with 3 slashes is necessary for Windows but will break on Unix.