Reputation: 8747
I want to make a PyQT4 window(QtGui.QMainWindow
) jump to the front when the application received a specified message from another machine.
Usually the window is minimized.
I tried the raise_()
and show()
method but it doesn't work.
Upvotes: 23
Views: 22428
Reputation: 361
To add to the answer from Zuzu Corneliu. I also tried everything and failed but what worked for me was to minimize the window and then bring it back from being minimized. Although the method zuzu used didn't work for me for some reason so I had to do something else.
I added this method to my QDialog / QMainWindow:
def reShow(self):
self.showMinimized()
self.setWindowState(self.windowState() and (not QtCore.Qt.WindowMinimized or QtCore.Qt.WindowActive))
Upvotes: 1
Reputation: 1703
For whoever is exasperated by this issue and didn't manage to have it fixed using either methods given as answers (e.g. for me nothing worked with PyQt5 if I clicked on another window immediately after starting the python script but before the Qt window appeared), here's what trick finally worked for me:
wnd.showMinimized() # this is the trick: minimize first
wnd.show() # or wnd.showMaximized() if you want it shown maximized
Upvotes: 0
Reputation: 15777
For me on Windows 10 with an NVidia GPU, this worked:
from PyQt4 import QtCore
# create window here...
window.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
I found it over on this answer: https://stackoverflow.com/a/12280956/4549682
Upvotes: 1
Reputation: 151
This works for me to raise the window but NOT have it on top all the time:
# 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)
Upvotes: 15
Reputation: 89
I didn't have any luck with the above methods, ended up having to use the win32 api directly, using a hack for the C version here. This worked for me:
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()
Upvotes: 5
Reputation: 36715
This works:
# 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()
Both are required for me on Win7.
setWindowState
restores the minimized window and gives focus. But if the window just lost focus and not minimized, it won't give focus.
activateWindow
gives focus but doesn't restore the minimized state.
Using both has the desired effect.
Upvotes: 24