Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDesktopServices.openUrl fails to open local file on Ubuntu
Forum Updated to NodeBB v4.3 + New Features

QDesktopServices.openUrl fails to open local file on Ubuntu

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    efremdan1
    wrote on 11 Mar 2021, 15:12 last edited by efremdan1 3 Nov 2021, 17:10
    #1

    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, running gio 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 run gio open file:doc/_build/latex/xslide-user-manual.pdf instead of gio open doc/_build/latex/xslide-user-manual.pdf (that is, Qt is adding "file:" to the command).

    Looks like a bug, right? Any workarounds?

    E 1 Reply Last reply 11 Mar 2021, 17:44
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 11 Mar 2021, 17:32 last edited by JoeCFD 3 Nov 2021, 17:40
      #5

      @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.pdf

      QDesktopServices.openUrl( QUrl::fromLocalFile( "doc/_build/latex/xslide-user-manual.pdf") );

      1 Reply Last reply
      0
      • E Offline
        E Offline
        efremdan1
        wrote on 11 Mar 2021, 15:16 last edited by
        #2

        Using Ubuntu 20.04.2 LTS and PySide2 5.15.2.

        J 1 Reply Last reply 11 Mar 2021, 16:45
        0
        • E efremdan1
          11 Mar 2021, 15:16

          Using Ubuntu 20.04.2 LTS and PySide2 5.15.2.

          J Offline
          J Offline
          JonB
          wrote on 11 Mar 2021, 16:45 last edited by
          #3

          @efremdan1

          QDesktopServices.openUrl(QUrl('file::doc/_build/latex/xslide-user-manual.pdf'))

          The only examples I can see use QUrl("file:..."), not QUrl("file::...")?

          Try it without the file::?

          1 Reply Last reply
          0
          • E Offline
            E Offline
            efremdan1
            wrote on 11 Mar 2021, 17:10 last edited by efremdan1 3 Nov 2021, 17:12
            #4

            @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.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JoeCFD
              wrote on 11 Mar 2021, 17:32 last edited by JoeCFD 3 Nov 2021, 17:40
              #5

              @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.pdf

              QDesktopServices.openUrl( QUrl::fromLocalFile( "doc/_build/latex/xslide-user-manual.pdf") );

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 11 Mar 2021, 17:43 last edited by
                #6

                @efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                file:doc/_build/latex/xslide-user-manual.pdf

                This no valid url

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 11 Mar 2021, 18:47
                0
                • E efremdan1
                  11 Mar 2021, 15:12

                  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, running gio 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 run gio open file:doc/_build/latex/xslide-user-manual.pdf instead of gio open doc/_build/latex/xslide-user-manual.pdf (that is, Qt is adding "file:" to the command).

                  Looks like a bug, right? Any workarounds?

                  E Offline
                  E Offline
                  eyllanesc
                  wrote on 11 Mar 2021, 17:44 last edited by eyllanesc 3 Nov 2021, 17:45
                  #7

                  @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")
                  

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  E 1 Reply Last reply 12 Mar 2021, 15:12
                  0
                  • C Christian Ehrlicher
                    11 Mar 2021, 17:43

                    @efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                    file:doc/_build/latex/xslide-user-manual.pdf

                    This no valid url

                    J Offline
                    J Offline
                    JonB
                    wrote on 11 Mar 2021, 18:47 last edited by
                    #8

                    @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);
                    
                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 11 Mar 2021, 19:17 last edited by
                      #9

                      @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>://.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      J 1 Reply Last reply 11 Mar 2021, 20:03
                      2
                      • C Christian Ehrlicher
                        11 Mar 2021, 19:17

                        @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>://.

                        J Offline
                        J Offline
                        JonB
                        wrote on 11 Mar 2021, 20:03 last edited by
                        #10

                        @Christian-Ehrlicher
                        Oh, I didn't realize the // is a compulsory part of any schema!

                        C 1 Reply Last reply 11 Mar 2021, 20:07
                        0
                        • J JonB
                          11 Mar 2021, 20:03

                          @Christian-Ehrlicher
                          Oh, I didn't realize the // is a compulsory part of any schema!

                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 11 Mar 2021, 20:07 last edited by
                          #11

                          @JonB see e.g. here: https://doc.qt.io/qt-5/qurl.html#setAuthority - :// must be between the schema and the 'rest' :)

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          J 1 Reply Last reply 11 Mar 2021, 20:14
                          2
                          • C Christian Ehrlicher
                            11 Mar 2021, 20:07

                            @JonB see e.g. here: https://doc.qt.io/qt-5/qurl.html#setAuthority - :// must be between the schema and the 'rest' :)

                            J Offline
                            J Offline
                            JonB
                            wrote on 11 Mar 2021, 20:14 last edited by
                            #12

                            @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>:/?

                            J 1 Reply Last reply 11 Mar 2021, 20:20
                            0
                            • J JonB
                              11 Mar 2021, 20:14

                              @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>:/?

                              J Offline
                              J Offline
                              JoeCFD
                              wrote on 11 Mar 2021, 20:20 last edited by JoeCFD 3 Nov 2021, 20:20
                              #13

                              @JonB it could be http:// and local file is file://

                              J 1 Reply Last reply 11 Mar 2021, 20:25
                              0
                              • J JoeCFD
                                11 Mar 2021, 20:20

                                @JonB it could be http:// and local file is file://

                                J Offline
                                J Offline
                                JonB
                                wrote on 11 Mar 2021, 20:25 last edited by
                                #14

                                @JoeCFD
                                My question is why didn't they go for, say, http:/ and file:/, instead of //, in the first place? Purely because I'm interested/wonder.

                                J 1 Reply Last reply 11 Mar 2021, 21:22
                                0
                                • J JonB
                                  11 Mar 2021, 20:25

                                  @JoeCFD
                                  My question is why didn't they go for, say, http:/ and file:/, instead of //, in the first place? Purely because I'm interested/wonder.

                                  J Offline
                                  J Offline
                                  JoeCFD
                                  wrote on 11 Mar 2021, 21:22 last edited by
                                  #15

                                  @JonB sorry I misundestood your previous post. / is reserved for path.
                                  check here https://en.wikipedia.org/wiki/URL out.

                                  J 1 Reply Last reply 12 Mar 2021, 08:30
                                  0
                                  • J JoeCFD
                                    11 Mar 2021, 21:22

                                    @JonB sorry I misundestood your previous post. / is reserved for path.
                                    check here https://en.wikipedia.org/wiki/URL out.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 12 Mar 2021, 08:30 last edited by JonB 3 Dec 2021, 08:30
                                    #16

                                    @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"! :)

                                    1 Reply Last reply
                                    0
                                    • E eyllanesc
                                      11 Mar 2021, 17:44

                                      @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")
                                      
                                      E Offline
                                      E Offline
                                      efremdan1
                                      wrote on 12 Mar 2021, 15:12 last edited by
                                      #17

                                      @eyllanesc said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                                      @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")

                                      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.

                                      E J J 3 Replies Last reply 12 Mar 2021, 15:15
                                      0
                                      • E efremdan1
                                        12 Mar 2021, 15:12

                                        @eyllanesc said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                                        @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")

                                        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.

                                        E Offline
                                        E Offline
                                        eyllanesc
                                        wrote on 12 Mar 2021, 15:15 last edited by eyllanesc 3 Dec 2021, 15:16
                                        #18

                                        @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 as python foo/script.py it may fail. Relative paths are one of the most common causes of silent errors that are the worst at debugging.

                                        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                                        1 Reply Last reply
                                        3
                                        • E efremdan1
                                          12 Mar 2021, 15:12

                                          @eyllanesc said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                                          @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")

                                          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.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 12 Mar 2021, 15:22 last edited by
                                          #19

                                          @efremdan1 said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                                          QUrl.fromLocalFile

                                          Using QUrl.fromLocalFile(), instead of trying to type in the right file:// stuff, is fine and good.

                                          However, passing a relative path to it, or specifying file:://relative-path, is dangerous precisely as @eyllanesc has said.

                                          1 Reply Last reply
                                          0
                                          • E efremdan1
                                            12 Mar 2021, 15:12

                                            @eyllanesc said in QDesktopServices.openUrl fails to open local file on Ubuntu:

                                            @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")

                                            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.

                                            J Offline
                                            J Offline
                                            JoeCFD
                                            wrote on 12 Mar 2021, 17:37 last edited by
                                            #20

                                            @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 Reply Last reply
                                            0

                                            5/21

                                            11 Mar 2021, 17:32

                                            topic:navigator.unread, 16
                                            • Login

                                            • Login or register to search.
                                            5 out of 21
                                            • First post
                                              5/21
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved