3
votes

I do really need to make my window to stay on top on Windows, but Windows itself does not seem willing to allow me to do this.

I cannot use the workaround with setting registry values because I am not able to ask the user to log-out/log-in.

Besides, I use QML and the solution with QWidget::raise() and QApplication::setActiveWindow() does not seem to work also because I have not managed to get the QML root object as a QWidget pointer with the following code:

QWidget* mainWin = qobject_cast<QWidget*>(engine.rootObjects().at(0));
if (mainWin)
{
    mainWin->raise();
    QApplication::setActiveWindow(mainWin);
    mainWin->activateWindow();
}

I have also tried to make the window active right from the QML:

window.raise()
window.requestActivate()

but with no luck also.

Is there, either way to bring the window on top on Windows without changing the registry and, preferably, from the QML purely?

Edit: currently used window flags are:

Qt.Popup
Qt.FramelessWindowHint
Qt.WindowStaysOnTopHint
Qt.CustomizeWindowHint
Qt.BypassWindowManagerHint
Qt.MSWindowsFixedSizeDialogHint

I am deploying Qt 5.7 app on the Windows 10 x64 machine. I have found this two bugfixes:

from which I can conclude that QWidget::activateWindow() and QWindow::requestActive() should work on Windows XP and Windows 7.

Here is my mcve, as @derM asked:

import QtQuick 2.7
import QtQuick.Window 2.2

Window {
    flags: Qt.WindowStaysOnTopHint

    width: 100
    height: 100
    visible: true
}

It was compiled under Windows 10 x64 with MinGW x32.

Easier way to reproduce: run in the command prompt

timeout 5 && debug\Test.exe

where debug\Test.exe is a path to the mcve binary, then open File Explorer and navigate somewhere. When the window opens, it won`t be in the foreground.

Harder way: If you just run it, the window will stay on top as it should. But if you press Run button in the Qt Creator and switch the focus (I suppose, mouse focus should be changed, just pressing Alt+Tab won`t help) to another process (in my case - File Explorer), the window is displayed under the current active File Explorer window, and even if I will bring it up by clicking, it will go background as soon as I choose any other application.

The real application is started from a service, so there often will be an app holding mouse focus when my app is started. I suppose that Qt ability to bring the window to foreground is implemented using SetForegroundWindow API call, which notes the following restrictions in it`s remarks:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The process is being debugged.
  • The foreground process is not a Modern Application or the Start Screen.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see - SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

So I wonder is it possible at all to bring the window to foreground if the process has been started by a service, not a user (i. e. the process has not been an active process during startup).

1
Just to make sure: You are talking about QML not about QtWidgets, right?derM
@derM Exactly, but I assume that there is a common mechanics inside QML types and QWidgets, that`s why I have paid attention to the bugs related to QWidgets. I have found nothing but the Qt.WindowStaysOnTopHint flag that could help with the problem from pure QML side.Oleg Lokshyn
Ok, there seems to be a bug indeed. I will post some hack that circumvents it for me.derM

1 Answers

6
votes

As you have posted: If the window is created from the foreground process, we will just make sure, that our process is the foreground process before we create the window.

import QtQuick 2.7
import QtQuick.Window 2.2

Item {
    id: root

    Component { // Like a splash screen: Claim to be foreground process,
                // then create main window.
        id: winInit
        Window {
            flags: Qt.WindowStaysOnTopHint
            width: 1
            height: 1
            Component.onCompleted: {
                requestActivate()
                mainWin.createObject(root)
            }
        }
    }

    Component {
        id: mainWin
        Window {
            flags: Qt.WindowStaysOnTopHint
            width: 100
            height: 100
            visible: true
        }
    }

    Component.onCompleted: {
        var w1 = winInit.createObject(null)
        w1.destroy()
    }
}