PyQt: Qt.Popup widget sometimes loses focus without closing, becomes unlocked

I am writing a very small application with PyQt. All my tests have been on Ubuntu / gnome so far.

I want one Pop-up style window, without entering a taskbar / panel that closes itself (and the application) at the moment of losing focus.

The Qt.Popup flag seems to match the score, but I had a strange problem. I noticed that it is possible (quite simply, in fact) to divert attention from the application when it starts, leaving the pop-up window without focus - and now it cannot be closed because it cannot lose focus.

Here is a simplified example:

#!/usr/bin/python import sys from PyQt4.QtCore import * from PyQt4.QtGui import * if __name__ == '__main__': app = QApplication(sys.argv) w = QDialog() w.setWindowFlags(Qt.Popup) w.exec_() 

If you press a little at the same moment when the program starts, QDialog will appear without focus and will not close under any circumstances. Clicking on the popup does not restore focus or allows you to close it.

I could add a close button to the pop-up window (and I intend!), But this does not eliminate the “close to lost focus” disruption. Is there anything else I should do with Qt.Popup windows to prevent this, or is there a way I can get around it?

+4
source share
1 answer

Using QWidget :: raise () seems to help here. (Also took the liberty and fixed the application event loop)

 #!/usr/bin/python import sys #import time from PyQt4.QtCore import * from PyQt4.QtGui import * if __name__ == '__main__': #time.sleep(2) app = QApplication(sys.argv) w = QDialog() w.setWindowFlags(Qt.Popup) w.setAttribute(Qt.WA_QuitOnClose) w.show() w.raise_() sys.exit(app.exec_()) 
+4
source

All Articles