4

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?

Trip Volpe
  • 210
  • 2
  • 8

1 Answers1

4

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_())
aukaost
  • 3,778
  • 1
  • 24
  • 26
  • Ah, thanks for the event loop bit; I used QDialog::exec because QApplication::exec didn't want to exit automatically when a Popup window was closed... I didn't know about WA_QuitOnClose. :P The problem still seems to persist, though; I also wrote an equivalent C++ program and it was susceptible to the same issue. I wonder if this is a Qt bug? – Trip Volpe Jul 13 '11 at 18:15
  • 1
    I tried this on a VM running some Ubuntu on Gnome where I could reproduce your problem using your example. I also tried your example on a Windows 7 machine where I wasn't able to reproduce this behavior. So I'd assume that something between Qt and the window manager is not harmonizing so well. My example seems to be working though on my Ubuntu VM setup. – aukaost Jul 13 '11 at 22:52
  • 1
    Yeah, that seems likely. Thanks for your effort! I guess I'll take this up on the Qt mailing list and/or bug tracker. – Trip Volpe Jul 14 '11 at 00:03