PyQt:Qt.Popup 小部件有时会在未关闭的情况下失去焦点,变得无法关闭
我正在使用 PyQt 编写一个非常小的应用程序。到目前为止,我所有的测试都是在 Ubuntu/gnome 上进行的。
我想要一个“弹出”样式的窗口,没有任务栏/面板条目,它会在失去焦点时自行关闭(和应用程序)。
Qt.Popup 标志似乎符合要求,但我遇到了一个奇怪的问题。我注意到,有可能(实际上很容易)在应用程序启动时将焦点从应用程序上移开,留下没有焦点的弹出窗口 - 现在不可能关闭它,因为它不能失去焦点。
这是一个简化的示例:
#!/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_()
如果您在程序启动的同一时刻单击一下,QDialog 将出现没有焦点,并且在任何情况下都不会自行关闭。单击弹出窗口不会恢复焦点或允许其关闭。
我可以向弹出窗口添加一个关闭按钮(我打算这样做!),但这并不能修复损坏的关闭丢失焦点行为。我还应该对 Qt.Popup 窗口做其他事情来防止这种情况,或者有什么方法可以解决它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 QWidget::raise() 似乎在这里有帮助。
(还冒昧修复了您的应用程序事件循环)
Using QWidget::raise() seems to help here.
(Also took the liberty and fixed your app event loop)