[Qt] 通过socket将另一个程序的某个窗口调到最前端
@ // THIS IS A HACK: // from QT documentation: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // void QWidget::activateWindow () // ... // On Windows, if you are calling this when the application is not currently // the active one then it will not make it the active window. It will change // the color of the taskbar entry to indicate that the window has changed in // some way. This is because Microsoft do not allow an application to // interrupt what the user is currently doing in another application. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // This hack does not give the focus to the app but brings it to front so // the user sees it. ::SetWindowPos(effectiveWinId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); ::SetWindowPos(effectiveWinId(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); // HACK END
In linux i use this:
if(!this->isActiveWindow())
{ this->setWindowFlags((windowFlags() & Qt::WindowStaysOnTopHint)); this->setWindowFlags((windowFlags() & ~Qt::WindowStaysOnTopHint)); this->show();
}
I use this in windows, it works
Qt::WindowFlags flags = windowFlags(); this->setWindowFlags((flags | Qt::WindowStaysOnTopHint)); this->show(); this->setWindowFlags(flags); this->show();
出处:https://forum.qt.io/topic/1939/activatewindow-does-not-send-window-to-front/11