#1152359 okular: Host portal registration fails when desktopFileName is set after QGuiApplication is constructed

Package:
okular
Source:
okular
Description:
universal document viewer
Submitter:
Andrew Atkinson
Date:
2026-10-28 14:03:02 UTC
Severity:
normal
#1152359#5
Date:
2026-10-28 14:00:55 UTC
From:
To:
Dear Maintainer,

*** Reporter, please consider answering these questions, where appropriate ***

   * What led up to the situation?
Open okular

Then asked AI to do a check. It reported

If QGuiApplication::setDesktopFileName() is called after the QGuiApplication
object is created, registration with org.freedesktop.host.portal.Registry fails
every time and Qt prints

qt.qpa.services: Failed to register with host portal
QDBusError("org.freedesktop.portal.Error.Failed", "Could not register app ID:
Connection already associated with an application ID")

By the time Register is called, Qt has already called
org.freedesktop.portal.Settings.ReadAll (for the colour scheme and
accessibility settings) on the same D-Bus connection. xdg-desktop-portal fixes
a connection's app ID at its first portal call, so the later Register is
refused.

Setting the desktop file name after construction is common.
KAboutData::setApplicationData() does it, so every KDE application hits this.
Okular 26.08.2 (Debian 4:26.08.2-1, KF 6.30.0) shows the warning on each start,
as did 26.08.0, and CopyQ reports the same thing
(https://github.com/hluk/CopyQ/issues/3631).

Steps to reproduce:
1. Build the program below against Qt 6.11.2 (g++ main.cpp $(pkg-config
--cflags --libs Qt6Gui) -fPIC).
2. Run it with QT_LOGGING_RULES="qt.qpa.services*=true".
3. Watch the portal calls with dbus-monitor --session
"destination=org.freedesktop.portal.Desktop".

#include <QGuiApplication>
#include <QTimer>
int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);
    QGuiApplication::setDesktopFileName("org.kde.okular"); // any installed
desktop ID
    QTimer::singleShot(1000, &app, &QGuiApplication::quit);
    return app.exec();
}

Actual result:
Portal calls in this order
  org.freedesktop.DBus.Properties.Get
  org.freedesktop.portal.Settings.ReadAll
  org.freedesktop.portal.Settings.ReadAll
  org.freedesktop.host.portal.Registry.Register  -> "Connection already
associated with an application ID"

Expected result:
Registration succeeds, as it does when setDesktopFileName() is called before
QGuiApplication is constructed. In that case Register comes straight after
Properties.Get and Qt logs
  qt.qpa.services: Successfully registered with host portal as "org.kde.okular"

Possible fixes:
Defer the Settings portal reads until desktopFileName is known (or the event
loop starts), or register on a separate D-Bus connection. Otherwise document
that desktopFileName must be set before construction, and do not attempt a
registration that cannot succeed.