Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDesktopServices::openUrl() not working if app closes
Qt 6.12 LTS is out! See what's new in the release blog

QDesktopServices::openUrl() not working if app closes

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 153 Views 1 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.
  • J Online
    J Online
    jayrickaby
    wrote last edited by
    #1

    Hello! I am making an app similar to the Window 7 Run Dialog. For that, the file/folder/website/other is opened and the window closes.

    I have noticed that opening the file/folder/website via QDesktopServices::openUrl() works fine until I want to close the application immediately after. If I do that, the item to open is never opened. I thought this was an issue with the application portal (which may open for some file types), as I have found that is attached to my app's process for whatever reason. However, even with file types with default applications, they don't open if the app is closed after calling openUrl().

    Is there anything I can do about this, such as waiting for the portal/process to properly start before closing the app? Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      Which OS are you on ?
      Which Qt version are you using ?

      Note, as the QDesktopServices details show, there's no guarantee that the application starts.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Online
        J Online
        jayrickaby
        wrote last edited by
        #3

        Qt v6.12.0-4, Linux (EndeavourOS, KDE Plasma)
        I've looked at possibly using QProcess::startDetached() instead, but I don't know if that would open a file/folder/website with its default app in an easy cross-platform way

        I 1 Reply Last reply
        0
        • J jayrickaby

          Qt v6.12.0-4, Linux (EndeavourOS, KDE Plasma)
          I've looked at possibly using QProcess::startDetached() instead, but I don't know if that would open a file/folder/website with its default app in an easy cross-platform way

          I Offline
          I Offline
          IgKh
          wrote last edited by
          #4

          A possible theory is that when QDesktopServices invokes the XDG Desktop Portal service, it passes a reference to your application's active window. It is needed because the portal might decide to present a confirmation dialog or an application chooser dialog, which it needs to parent to your application's window. It is possible that KDE's implementation still fails if this reference is invalid, even if it doesn't actually need to display a dialog. I didn't test this theory, but if true - do note that it is within it's rights to do so.

          A quick thing you can do as a workaround is to start xdg-open as an attached sub-process. It works in Flatpak too.

          I 1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by
            #5

            I don't know what the code of QDesktopServices::openUrl() does. But another possibility is that if you exit the program immediately after invoking it then it simply does not get enough time to do whatever to spawn the process, depending on its implementation. The first thing I would do is put in a sleep/delay before closing your application and see whether (a) that allows the sub-process to run and continue running or (b) allows the sub-process to run but then it "terminates" when you do exit your parent application. Then you will know where the issue lies.

            1 Reply Last reply
            0
            • I IgKh

              A possible theory is that when QDesktopServices invokes the XDG Desktop Portal service, it passes a reference to your application's active window. It is needed because the portal might decide to present a confirmation dialog or an application chooser dialog, which it needs to parent to your application's window. It is possible that KDE's implementation still fails if this reference is invalid, even if it doesn't actually need to display a dialog. I didn't test this theory, but if true - do note that it is within it's rights to do so.

              A quick thing you can do as a workaround is to start xdg-open as an attached sub-process. It works in Flatpak too.

              I Offline
              I Offline
              IgKh
              wrote last edited by IgKh
              #6

              @IgKh said in QDesktopServices::openUrl() not working if app closes:

              A possible theory is that when QDesktopServices invokes the XDG Desktop Portal service, it passes a reference to your application's active window. It is needed because the portal might decide to present a confirmation dialog or an application chooser dialog, which it needs to parent to your application's window. It is possible that KDE's implementation still fails if this reference is invalid, even if it doesn't actually need to display a dialog. I didn't test this theory, but if true - do note that it is within it's rights to do so.

              Now I did test it, and that theory was not quite right. On Wayland, the calling application needs to request an activation token from the compositor to properly pass focus from itself to the spawned application. The communication with the compositor is asynchronous, and requires the event loop to keep running for a while to receive the token and actually invoke the portal or spawn the target binary. So it is not something specific to KDE.

              I doesn't matter much for @jayrickaby though, and the suggestion to do it out of process with xdg-open stands (just that doing it that way may not allow the opened application to automatically get focus on some compositors, if that matters)

              1 Reply Last reply
              0
              • J Online
                J Online
                jayrickaby
                wrote last edited by
                #7

                okay, i guess that i'll target xdg-open when the user is on linux. thanks for the help. i was hoping for something easily cross-distro (or even not bound to xdg-open) like QDesktopServices::openUrl(). I'll have a look at the whole authorisation stuff and apply that to wayland and other sessions. thanks all
                if there's an easier way for this that anyone knows of i will be happy to hear

                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
                • Unsolved