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() not working correctly
Forum Updated to NodeBB v4.3 + New Features

QDesktopServices::openUrl() not working correctly

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 3.1k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    Trino
    wrote on last edited by Trino
    #1

    Consider the following code:

    void UI_myapp::helpbuttonpressed()
    {
    #ifdef Q_OS_LINUX
      QDesktopServices::openUrl(QUrl("file:///usr/share/doc/myapp/manual.html#name_of_anchor"));
    #endif
    
    #ifdef Q_OS_WIN32
      char p_path[MAX_PATH_LENGTH];
    
      strcpy(p_path, "file:///");
      strcat(p_path, mainwindow->specialFolder(CSIDL_PROGRAM_FILES).toLocal8Bit().data());
      strcat(p_path, "\\myapp\\manual.html#name_of_anchor");
      QDesktopServices::openUrl(QUrl(p_path));
    #endif
    }
    

    On Linux it works as expected, it opens the html file with the default browser and jumps
    to the specified anchor.

    On windows however, it opens the html file but it ignores the specified anchor.

    Qt version is 4.8.7.

    Is this normal behaviour, a bug or is there something wrong with my code?

    raven-worxR 1 Reply Last reply
    0
    • T Trino

      Consider the following code:

      void UI_myapp::helpbuttonpressed()
      {
      #ifdef Q_OS_LINUX
        QDesktopServices::openUrl(QUrl("file:///usr/share/doc/myapp/manual.html#name_of_anchor"));
      #endif
      
      #ifdef Q_OS_WIN32
        char p_path[MAX_PATH_LENGTH];
      
        strcpy(p_path, "file:///");
        strcat(p_path, mainwindow->specialFolder(CSIDL_PROGRAM_FILES).toLocal8Bit().data());
        strcat(p_path, "\\myapp\\manual.html#name_of_anchor");
        QDesktopServices::openUrl(QUrl(p_path));
      #endif
      }
      

      On Linux it works as expected, it opens the html file with the default browser and jumps
      to the specified anchor.

      On windows however, it opens the html file but it ignores the specified anchor.

      Qt version is 4.8.7.

      Is this normal behaviour, a bug or is there something wrong with my code?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Trino
      are you using the same browser on both platforms during testing?

      I wasn't aware that the stuff the anchor feature would work at all.
      On windows you can test this, by creating a custom shortcut/link (e.g. on the desktop) with the url and check if it behaves the same.
      If so there is nothing you can do. If it works also there then maybe Qt's internal implementation drops the anchor somewhere.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      T 1 Reply Last reply
      0
      • miclandM Offline
        miclandM Offline
        micland
        wrote on last edited by
        #3

        Can you see the anchor in the location bar of your browser? If so the URL is completely passed to the browser and it sounds like a bug in your used browser... If the anchor it's not in the location bar it was dropped anywhere (by Qt? by the platform? ....) and the browser can't jump to the anchor.

        T 1 Reply Last reply
        0
        • jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Not an answer to your question: why do you use \ in strcat(p_path, "\myapp\manual.html#name_of_anchor");? You can use /
          Another thing: why do you use strcpy/strcat? Why not use QString which is more high level? In the end you're using C++ not C :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @Trino
            are you using the same browser on both platforms during testing?

            I wasn't aware that the stuff the anchor feature would work at all.
            On windows you can test this, by creating a custom shortcut/link (e.g. on the desktop) with the url and check if it behaves the same.
            If so there is nothing you can do. If it works also there then maybe Qt's internal implementation drops the anchor somewhere.

            T Offline
            T Offline
            Trino
            wrote on last edited by Trino
            #5

            @raven-worx
            Yes, I use the latest version of Firefox on both platforms.
            When I copy the link into "run" (by pressing windows-key + r), the result is the same.
            Looks like a windows issue.

            raven-worxR 1 Reply Last reply
            0
            • miclandM micland

              Can you see the anchor in the location bar of your browser? If so the URL is completely passed to the browser and it sounds like a bug in your used browser... If the anchor it's not in the location bar it was dropped anywhere (by Qt? by the platform? ....) and the browser can't jump to the anchor.

              T Offline
              T Offline
              Trino
              wrote on last edited by
              #6

              @micland
              No, the anchor isn't visible in the location bar of the browser.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                Not an answer to your question: why do you use \ in strcat(p_path, "\myapp\manual.html#name_of_anchor");? You can use /
                Another thing: why do you use strcpy/strcat? Why not use QString which is more high level? In the end you're using C++ not C :-)

                T Offline
                T Offline
                Trino
                wrote on last edited by
                #7

                @jsulm
                Because I prefer plain C over C++ :-)
                I only use C++ when I can't avoid it e.g. when dealing with Qt.

                1 Reply Last reply
                0
                • T Trino

                  @raven-worx
                  Yes, I use the latest version of Firefox on both platforms.
                  When I copy the link into "run" (by pressing windows-key + r), the result is the same.
                  Looks like a windows issue.

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @Trino said:

                  Looks like a windows issue.

                  yes, maybe.
                  Possibly Windows already cleans the url up before making the actual call.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  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