Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-138892

QDesktopServices::openUrl doesn't activate launched app when called from context menu item

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 6.9.1
    • GUI: Window management
    • None
    • openSUSE Tumbleweed with KDE Plasma
    • Linux/Wayland

      When QDesktopServices::openUrl is called from &QAction::triggered signal, it fails to activate launched app (e.g. file manager).

      Example:

      #include <QApplication>
      #include <QDebug>
      #include <QDesktopServices>
      #include <QDir>
      #include <QMenu>
      #include <QTimer>
      #include <QUrl>
      
      namespace {
      void openUrl() {
        qInfo() << "applicationState:" << qApp->applicationState();
        qInfo() << "focusWindow:" << qApp->focusWindow();
        QDesktopServices::openUrl(QUrl::fromLocalFile(QDir::rootPath()));
      }
      } // namespace
      
      int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
      
        QWidget window{};
        window.resize(640, 480);
      
        window.setContextMenuPolicy(Qt::CustomContextMenu);
        QObject::connect(&window, &QWidget::customContextMenuRequested, &window,
                         [&](QPoint pos) {
                           const auto menu = new QMenu(&window);
                           menu->setAttribute(Qt::WA_DeleteOnClose);
                           const auto action = menu->addAction("Do it");
                           QObject::connect(action, &QAction::triggered, action, [] {
                             openUrl();
                             // QTimer::singleShot(100, &openUrl);
                           });
                           menu->popup(window.mapToGlobal(pos));
                         });
      
        window.show();
      
        return a.exec();
      }
      

      1. Launch this app
      2. Right click on the window and click on menu item

      File manager is launched, but, if it was already opened, its window is not activated.
      If openUrl is called with delay (e.g. 100 ms) then activation works.

      It seems that following check fails:
      https://github.com/qt/qtbase/blob/e1ecc3ae11e01f6a6601b921efb588e13b8baf3b/src/gui/platform/unix/qdesktopunixservices.cpp#L445

      QGuiApplication::focusWindow returns nullptr in that moment, despite the fact that application is active (and QGuiApplication::applicationState returns ApplicationActive)

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            vestbo Tor Arne Vestbø
            equeim Alexey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes