QDesktopServices.openUrl fails to open local file on Ubuntu
-
I'm using QDesktopServices.openUrl in my program to open a pdf version of the user manual when the user clicks "Help->Manual". I do this with
QDesktopServices.openUrl(QUrl('file:doc/_build/latex/xslide-user-manual.pdf'))
. This works fine when I run it on my Mac and my Windows machines, but when I run it on my Ubuntu machine I get the error:
gio: file:doc/_build/latex/xslide-user-manual.pdf: Operation not supported
The command used in Ubuntu's terminal to open this file is
gio open doc/_build/latex/xslide-user-manual.pdf
, and that works just fine from terminal. However, runninggio open file:doc/_build/latex/xslide-user-manual.pdf
from terminal gives the same error as Qt. So it looks to me like Qt is telling Ubuntu to rungio open file:doc/_build/latex/xslide-user-manual.pdf
instead ofgio open doc/_build/latex/xslide-user-manual.pdf
(that is, Qt is adding "file:" to the command).Looks like a bug, right? Any workarounds?
-
@efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:
gio open
auto url = QUrl::fromLocalFile( path + file_name );
gio open file:// + path + name will do it.
gio open file://doc/_build/latex/xslide-user-manual.pdfQDesktopServices.openUrl( QUrl::fromLocalFile( "doc/_build/latex/xslide-user-manual.pdf") );
-
@JonB said in QDesktopServices.openUrl fails to open local file on Ubuntu:
The only examples I can see use QUrl("file:..."), not QUrl("file::...")?
Try it without the file::?I'm sorry, that was a typo in my forum post. I am using
QUrl("file:...")
. I'll edit the post to reflect that. -
@efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:
gio open
auto url = QUrl::fromLocalFile( path + file_name );
gio open file:// + path + name will do it.
gio open file://doc/_build/latex/xslide-user-manual.pdfQDesktopServices.openUrl( QUrl::fromLocalFile( "doc/_build/latex/xslide-user-manual.pdf") );
-
@efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:
file:doc/_build/latex/xslide-user-manual.pdf
This no valid url
-
@efremdan1 use the fullpath:
import os.path CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(CURRENT_DIRECTORY, "doc/_build/latex/xslide-user-manual.pdf") print(filename) url = QUrl.fromLocalFile(filename) if not QDesktopServices.openUrl(url): print("failed")
-
@Christian-Ehrlicher said in QDesktopServices.openUrl fails to open local file on Ubuntu:
file:doc/_build/latex/xslide-user-manual.pdf
This no valid url
Do you mean because of the "relative" start to the path in
file:doc/..."
?@efremdan1
You can do as @eyllanesc and others say and make your file path absolute before you start.You can also read https://doc.qt.io/qt-5/qurl.html#fromLocalFile where the docs explain about local, relative files and URLs, especially the bottom example code there:
For this reason, it is better to use a relative URL (that is, no scheme) for relative file paths:
QUrl url = QUrl("file.txt"); QUrl baseUrl = QUrl("file:/home/user/"); // prints QUrl("file:///home/user/file.txt") qDebug() << baseUrl.resolved(url);
-
@JonB said in QDesktopServices.openUrl fails to open local file on Ubuntu:
Do you mean because of the "relative" start to the path in file:doc/..."?
No, it's simply no valid url since the schema must be
<name>://
. -
@Christian-Ehrlicher
Oh, I didn't realize the//
is a compulsory part of any schema! -
@JonB see e.g. here: https://doc.qt.io/qt-5/qurl.html#setAuthority -
://
must be between the schema and the 'rest' :) -
@Christian-Ehrlicher
Yes, thank you. I am not sure I had ever particularly been struck by that, but I see now!So the obvious question might be: why did they need
//
? there must be a reason. Why not just/
? Why didn't the first people who defined this settle on<name>:/
? -
@JoeCFD said in QDesktopServices.openUrl fails to open local file on Ubuntu:
/ is reserved for path.
Again, I know that, my question was why adopt double-slashes. Interestingly (to me) I have come across a BBC interview from 2009 with Tim Berners-Lee where it states:
The forward slashes at the beginning of internet addresses have long annoyed net users and now the man behind them has apologised for using them.
Sir Tim Berners-Lee, the creator of the World Wide Web, has confessed that the // in a web address were actually "unnecessary".
He told the Times newspaper that he could easily have designed URLs not to have the forward slashes.
So there you are --- the choice of
//
was "annoying" and "unnecessary"! :) -
@eyllanesc said in QDesktopServices.openUrl fails to open local file on Ubuntu:
@efremdan1 use the fullpath:
import os.pathCURRENT_DIRECTORY = os.path.dirname(os.path.realpath(file))
filename = os.path.join(CURRENT_DIRECTORY, "doc/_build/latex/xslide-user-manual.pdf")
print(filename)
url = QUrl.fromLocalFile(filename)
if not QDesktopServices.openUrl(url):
print("failed")This answer works too, but I marked @JoeCFD's as the correct answer since it doesn't look necessary to use the absolute directly. Just using
QDesktopServices.openUrl(QUrl.fromLocalFile("doc/_build/latex/xslide-user-manual.pdf"))
works absolutely fine for me. And since it's a lot shorter and more readable, I don't see why not to use it. -
@efremdan1 I don't recommend using relative paths as they will depend on where the "current location" is. In your case it seems that you are executing the script of the form:
python script.py
but if at another time you execute it aspython foo/script.py
it may fail. Relative paths are one of the most common causes of silent errors that are the worst at debugging. -
@efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:
QUrl.fromLocalFile
Using
QUrl.fromLocalFile()
, instead of trying to type in the rightfile://
stuff, is fine and good.However, passing a relative path to it, or specifying
file:://relative-path
, is dangerous precisely as @eyllanesc has said.