windowsqtqqmlapplicationenginesetforegroundwindow

Force QApplication using QQmlApplicationEngine to be on foreground


I am trying to force my application to stay on top of everything even when other processes pop up. Here's a simplified version of my main:

main.cpp

QApplication                        app{argc, argv};
QQmlApplicationEngine               engine;

engine.load(QUrl{"qrc:/file.qml"});

return app.exec();

I need a solution for both Windows and Linux. However the priority is to the former and there doesn't seem to be a Qt solution. Here's what I tried:

#ifdef _WIN32
    HWND hCurWnd = ::GetForegroundWindow();
    DWORD dwMyID = ::GetCurrentThreadId();
    DWORD dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
    ::AttachThreadInput(dwCurID, dwMyID, TRUE);
    ::SetWindowPos(hCurWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    ::SetWindowPos(hCurWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    bool ok = ::SetForegroundWindow(hCurWnd);
    LOG_INFO() << ok;
    ::AttachThreadInput(dwCurID, dwMyID, FALSE);
    ::SetFocus(hCurWnd);
    ::SetActiveWindow(hCurWnd);
#endif

ok returns true but it doesn't seem to work. External processes still appear on top of the application after its start.

The loaded QML file has its visibility set on FullScreen. Its type is ApplicationWindow.


Solution

  • Nevermind it was dumb simple:

    setWindowFlags(Qt::WindowStaysOnTopHint) hides Qt Window

    Therefore I wrote this in my file.qml:

    ApplicationWindow
    {
        visibility: "FullScreen"
        flags: Qt.WindowStaysOnTopHint
    }