QDesktopServices.openUrl fails to open local file on Ubuntu
-
wrote on 11 Mar 2021, 15:12 last edited by efremdan1 3 Nov 2021, 17:10
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?
-
wrote on 11 Mar 2021, 17:32 last edited by JoeCFD 3 Nov 2021, 17:40
@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") );
-
wrote on 11 Mar 2021, 15:16 last edited by
Using Ubuntu 20.04.2 LTS and PySide2 5.15.2.
-
wrote on 11 Mar 2021, 16:45 last edited by
QDesktopServices.openUrl(QUrl('file::doc/_build/latex/xslide-user-manual.pdf'))
The only examples I can see use
QUrl("file:...")
, notQUrl("file::...")
?Try it without the
file::
? -
wrote on 11 Mar 2021, 17:10 last edited by efremdan1 3 Nov 2021, 17:12
@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. -
wrote on 11 Mar 2021, 17:32 last edited by JoeCFD 3 Nov 2021, 17:40
@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
-
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?
wrote on 11 Mar 2021, 17:44 last edited by eyllanesc 3 Nov 2021, 17:45@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")
-
@efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:
file:doc/_build/latex/xslide-user-manual.pdf
This no valid url
wrote on 11 Mar 2021, 18:47 last edited by@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>://
. -
@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>://
.wrote on 11 Mar 2021, 20:03 last edited by@Christian-Ehrlicher
Oh, I didn't realize the//
is a compulsory part of any schema! -
@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' :) -
@JonB see e.g. here: https://doc.qt.io/qt-5/qurl.html#setAuthority -
://
must be between the schema and the 'rest' :)wrote on 11 Mar 2021, 20:14 last edited by@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>:/
? -
@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
My question is why didn't they go for, say,http:/
andfile:/
, instead of//
, in the first place? Purely because I'm interested/wonder.wrote on 11 Mar 2021, 21:22 last edited by@JonB sorry I misundestood your previous post. / is reserved for path.
check here https://en.wikipedia.org/wiki/URL out. -
@JonB sorry I misundestood your previous post. / is reserved for path.
check here https://en.wikipedia.org/wiki/URL out.wrote on 12 Mar 2021, 08:30 last edited by JonB 3 Dec 2021, 08:30@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"! :) -
@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")
wrote on 12 Mar 2021, 15:12 last edited by@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. -
@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.wrote on 12 Mar 2021, 15:15 last edited by eyllanesc 3 Dec 2021, 15:16@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. -
@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.wrote on 12 Mar 2021, 15:22 last edited by@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. -
@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.wrote on 12 Mar 2021, 17:37 last edited by@efremdan1 The other guys are right. I thought you might know this and therefore did not mention that in my post. Try to use absolute path as much as possible.
1/21