如何让PyQT4窗口跳到前面?

red*_*ice 14 python pyqt4

QtGui.QMainWindow当应用程序从另一台机器收到指定的消息时,我想让PyQT4窗口()跳转到前面.通常窗口最小化.

我尝试了raise_()show()方法,但它不起作用.

Ava*_*ris 15

这有效:

# this will remove minimized status 
# and restore window with keeping maximized/normal state
window.setWindowState(window.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)

# this will activate the window
window.activateWindow()
Run Code Online (Sandbox Code Playgroud)

在Win7上我都需要这两个.

setWindowState恢复最小化的窗口并提供焦点.但是如果窗口失去了焦点并且没有最小化,它就不会给焦点.

activateWindow 给予焦点但不恢复最小化状态.

使用两者都具有期望的效果.


Kev*_*man 7

这对我来说可以升起窗户,但不能一直放在上面:

# bring window to top and act like a "normal" window!
window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)  # set always on top flag, makes window disappear
window.show() # makes window reappear, but it's ALWAYS on top
window.setWindowFlags(window.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint) # clear always on top flag, makes window disappear
window.show() # makes window reappear, acts like normal window now (on top now but can be underneath if you raise another window)
Run Code Online (Sandbox Code Playgroud)

  • 我尝试了此页面上的所有其他建议。这是唯一在 Windows 10 上对我有用的方法。 `window.setWindowFlags(window.windowFlags() | QtCore.Qt.WindowStaysOnTopHint); 窗口.show()` (3认同)

小智 5

我对上述方法没有任何运气,最终不得不直接使用 win32 api,在这里使用 C 版本的 hack 。这对我有用:

from win32gui import SetWindowPos
import win32con

SetWindowPos(window.winId(),
             win32con.HWND_TOPMOST, # = always on top. only reliable way to bring it to the front on windows
             0, 0, 0, 0,
             win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)
SetWindowPos(window.winId(),
             win32con.HWND_NOTOPMOST, # disable the always on top, but leave window at its top position
             0, 0, 0, 0,
             win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)
window.raise_()
window.show()
window.activateWindow()
Run Code Online (Sandbox Code Playgroud)