In my application, I generate a HTML file that I want to open by clicking on a button. So my file is named, for example:
QString file = "F:/the_path/to_the_/generated_html_file.html";
On Windows I change it to:
file = "file:///F:/the_path/to_the_/generated_html_file.html";
so that I can open it with :
QDesktopServices::openUrl(QUrl(file));
and it opens in the default browser.
But when the character # is present in the path or file name, it doesn't work any more and it seems that the URL is truncated just after the #.
For example, if I name the file generated#_html_file.html, I get this error message:
ShellExecute 'F:/the_path/to_the_/generated' failed (error 2).
Why does that happen, and how can I avoid it?
In a URL,
#is a character that delimits the 'fragment identifier' from the resource location. To reference afile:URL with a literal#, it needs to be escaped (as%23).Reference: RFC 1738:
As noted by SteveTJS, the static method
QUrl::fromLocalFile()is provided for this purpose, so you can writeinstead of
This will
file:protocol identifier and//empty hostname/(if different)