Pyside2 How To Get the window pops up to the front

Scenario
It’s a pyside2 program running in Liunx system. There is a menu that every item in it is a exec. Every time I clicked the item, the exec starts if it’s not started. The exec will pops up to the front if the exec is started. all the execs are External programs that I can’t let them show just by change the code.

approaches
I use the xprop command to get the QT window handle, then use the QWindow apis to let the window show. just like this:

window = QWindow.fromWindId(handle)
window.setFlags(window.flags() | Qt.WindowStaysOnTopHint)
window.show()

It works but has the problem that it always stays on top.
I change the code like this:

window = QWindow.fromWindId(handle)
window.setFlags(window.flags() | Qt.WindowStaysOnTopHint)
window.show()
window.setFlags(window.flags() & ~Qt.WindowStaysOnTopHint)
window.show()

It does’not work, How can I just let the window to pop up to the front, not always on the top?
By the way, the requestActivate() and raise() not works either.

  • What happens if you don’t set WindowStaysOnTopHint at all? Note that the docs explain that foreign native windows should not be manipulated through the QWindow interface (see the fromWinId() note). You may have some success by using QWidget.createWindowContainer(), but then you may have further issues related to focus and user input. In general, while it’s theoretically supported, embedding foreign windows is discouraged as it’s almost always unreliable.

    – 




  • The question is quite unclear, but it seems you are asking how to activate an external program that you’ve already started. Qt cannot function as a window-manager, so you must use a third-party utility to do this. On Linux, a good choice is xdotool. If you use QProcess.startDetached to start the program, it will return the PID, which can be used to get the WID, like this: xdotool search --pid PID. Then you can bring the window to the front like this: xdotool windowactivate WID.

    – 

Leave a Comment