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.1k 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 efremdan1

    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?

    eyllanescE Offline
    eyllanescE Offline
    eyllanesc
    wrote on last edited by eyllanesc
    #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
    0
    • Christian EhrlicherC Christian Ehrlicher

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

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

      This no valid url

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        JonBJ 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #10

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

          Christian EhrlicherC 1 Reply Last reply
          0
          • JonBJ JonB

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

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            JonBJ 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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>:/?

              JoeCFDJ 1 Reply Last reply
              0
              • JonBJ JonB

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

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #13

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

                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JonBJ JonB

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

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #15

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

                    JonBJ 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #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
                      • eyllanescE eyllanesc

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

                        eyllanescE JonBJ JoeCFDJ 3 Replies Last reply
                        0
                        • E efremdan1

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

                          eyllanescE Offline
                          eyllanescE Offline
                          eyllanesc
                          wrote on last edited by eyllanesc
                          #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

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

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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

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

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on 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
                              • E Offline
                                E Offline
                                efremdan1
                                wrote on last edited by
                                #21

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

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

                                You're indeed correct. Thank you.

                                1 Reply Last reply
                                0

                                • Login

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