4
votes

I'm writing a very small application with PyQt. All of my testing has been on Ubuntu/gnome so far.

I want a single "Popup" style window, with no taskbar/panel entry, that will close itself (and the application) the moment it loses focus.

The Qt.Popup flag seems to fit the bill, but I'm having a weird problem. I've noticed that it's possible (pretty easy, in fact) to take focus away from the application as it's starting, leaving a Popup window with no focus -- and it is now impossible to close it, because it cannot lose focus.

Here's 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 click around a bit within the same moment the program is starting, the QDialog will appear without focus, and will not close itself under any circumstance. Clicking on the popup does not restore focus or allow it to be closed.

I could add a close button to the popup (and I intend to!) but that doesn't fix the broken close-on-lost-focus behavior. Is there something else I should be doing with Qt.Popup windows to prevent this, or is there some way I can work around it?

1

1 Answers

4
votes

Using QWidget::raise() seems to help here. (Also took the liberty and fixed your app 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_())