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.
Sometimes you want to position a window in the middle or center of the computer screen, with PyQt you can do that. To center a Python PyQt window (put it in the center of the screen), we need to do a bit of trickery: we need to get the window properties, center point and move it ourself.
Creating a new window. In Qt any widget without a parent is a window. This means, to show a new window you just need to create a new instance of a widget. This can be any widget type (technically any subclass of QWidget ) including another QMainWindow if you prefer.
PyQt is a Python binding for Qt, which is a set of C++ libraries and development tools providing platform-independent abstractions for graphical user interfaces (GUIs). Qt also provides tools for networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other powerful features.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With