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 3.9k 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.
  • 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 Offline
      Christian EhrlicherC Offline
      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