title: QML on Windows: make the window to stay on top
body: <p>I do really need to make my window to stay on top on Windows, but Windows itself does not seem <a href="https://wiki.qt.io/Qt_project_org_faq#QWidget_::activateWindow.28.29_-_behavior_under_windows" rel="nofollow noreferrer">willing to allow me</a> to do this.</p>
  
  <p>I cannot use the workaround with setting registry values because I am not able to ask the user to log-out/log-in.</p>
  
  <p>Besides, I use QML and the solution with <a href="http://www.qtcentre.org/threads/22197-Bring-application-window-to-front" rel="nofollow noreferrer" title="QWidget::raise&#40;&#41; and QApplication::setActiveWindow&#40;&#41;">QWidget::raise() and QApplication::setActiveWindow()</a> 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:</p>
  
  <pre><code>QWidget* mainWin = qobject_cast&lt;QWidget*&gt;(engine.rootObjects().at(0));
  if (mainWin)
  {
      mainWin-&gt;raise();
      QApplication::setActiveWindow(mainWin);
      mainWin-&gt;activateWindow();
  }
  </code></pre>
  
  <p>I have also tried to make the window active right from the QML:</p>
  
  <pre><code>window.raise()
  window.requestActivate()
  </code></pre>
  
  <p>but with no luck also.</p>
  
  <p>Is there, either way to bring the window on top on Windows without changing the registry and, preferably, from the QML purely?</p>
  
  <p><strong>Edit</strong>:
  currently used window flags are:</p>
  
  <pre><code>Qt.Popup
  Qt.FramelessWindowHint
  Qt.WindowStaysOnTopHint
  Qt.CustomizeWindowHint
  Qt.BypassWindowManagerHint
  Qt.MSWindowsFixedSizeDialogHint
  </code></pre>
  
  <p>I am deploying <strong>Qt 5.7 app on the Windows 10 x64</strong> machine.
  I have found this two bugfixes:</p>
  
  <ul>
  <li><a href="https://bugreports.qt.io/browse/QTBUG-14062" rel="nofollow noreferrer">https://bugreports.qt.io/browse/QTBUG-14062</a></li>
  <li><a href="https://bugreports.qt.io/browse/QTBUG-37435" rel="nofollow noreferrer">https://bugreports.qt.io/browse/QTBUG-37435</a></li>
  </ul>
  
  <p>from which I can conclude that QWidget::activateWindow() and QWindow::requestActive() should work on Windows XP and Windows 7.</p>
  
  <p>Here is my <strong>mcve</strong>, as @derM asked:</p>
  
  <pre><code>import QtQuick 2.7
  import QtQuick.Window 2.2
  
  Window {
      flags: Qt.WindowStaysOnTopHint
  
      width: 100
      height: 100
      visible: true
  }
  </code></pre>
  
  <p>It was compiled under Windows 10 x64 with MinGW x32.</p>
  
  <p><strong>Easier way</strong> to reproduce: run in the command prompt</p>
  
  <pre><code>timeout 5 &amp;&amp; debug\Test.exe
  </code></pre>
  
  <p>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. </p>
  
  <p><strong>Harder way</strong>: 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.</p>
  
  <p>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 <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx" rel="nofollow noreferrer">SetForegroundWindow</a> API call, which notes the following restrictions in it`s remarks:</p>
  
  <ul>
  <li>The process is the foreground process.</li>
  <li>The process was started by the foreground process.</li>
  <li>The process received the last input event.</li>
  <li>There is no foreground process.</li>
  <li>The process is being debugged.</li>
  <li>The foreground process is not a Modern Application or the Start Screen.</li>
  <li>The foreground is not locked (see LockSetForegroundWindow).</li>
  <li>The foreground lock time-out has expired (see  -   SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).</li>
  <li>No menus are active.</li>
  </ul>
  
  <p>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).</p>