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

Support bringing apps to the foreground

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • Not Evaluated
    • None
    • 5.0.0
    • Widgets: Main Window
    • None

    Description

      Feature

      Right now Qt has no API to bring the current application to the foreground.

      Use case

      When debugging with Qt Creator, the app being debugged (the debuggee) usually has focus. But as soon as we hit a breakpoint, the app stops, and Qt Creator should become active/move to the foreground.

      Related

      The documentation seems to indicate that QWidget::activateWindow() does exactly this. However, at least on kwin it doesnt work as expected (also together with raise()).

      The other option is to use Qt::WindowStaysOnTopHint , but that prevents the user to change the order later on.

      Possible implementation

      Right now we're using following snippet:

      void AppMainWindow::raiseWindow()
      {
          setWindowState(windowState() & ~Qt::WindowMinimized);
          activateWindow();
          raise();
      
          // force window to the top
      #ifdef Q_OS_WIN
          WId winId = winId();
          DWORD foregroundPId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
          DWORD myPId = GetWindowThreadProcessId(winId, NULL);
          if (foregroundPId != myPId) {
              AttachThreadInput(foregroundPId, myPId, true);
              BringWindowToTop(winId);
              AttachThreadInput(foregroundPId, myPId, false);
          }
      #elif defined(Q_WS_X11)
          XEvent xev;
          xev.xclient.type = ClientMessage;
          xev.xclient.message_type = XInternAtom(QX11Info::display(), "_NET_ACTIVE_WINDOW", 1);
          xev.xclient.display = QX11Info::display();
          xev.xclient.window = winId();
          xev.xclient.format = 32;
          xev.xclient.data.l[0] = 2; // from pager/user
          xev.xclient.data.l[1] = QX11Info::appTime();
          xev.xclient.data.l[2] = 0; // currently active window
          xev.xclient.data.l[3] = 0; // unused
          xev.xclient.data.l[4] = 0; // unused
          XSendEvent(QX11Info::display(), QX11Info::appRootWindow(x11Info().screen()), False,
                     SubstructureRedirectMask | SubstructureNotifyMask, &xev);
      #endif
      }
      

      Attachments

        Issue Links

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

          Activity

            People

              kkohne Kai Köhne
              kkohne Kai Köhne
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes