简体   繁体   English

PyQt5正确破坏程序

[英]PyQt5 destroy a program properly

I'm using self.setWindowFlags(Qt.Tool) to hide the application on task bar. 我正在使用self.setWindowFlags(Qt.Tool)隐藏任务栏上的应用程序。 However, I realized that even I press x on the right-top corner of the program, it's still running on the background ( I see it on task manager/processes). 但是,我意识到,即使在程序的右上角按x ,它仍在后台运行(我在任务管理器/进程中看到它)。 So that means the program doesn't close properly. 因此,这意味着程序无法正常关闭。 Is this because I use self.setWindowFlags(Qt.Tool) ? 这是因为我使用了self.setWindowFlags(Qt.Tool)吗? How can I fix this, that flag option hide program's icon but even if I press x to close the program, it's still running on the background. 我该如何解决,该标志选项隐藏了程序的图标,但是即使我按x关闭程序,它仍在后台运行。

In Qt an application will continue to run until the main event loop receives the signal to exit. 在Qt中,应用程序将继续运行,直到主事件循环收到退出信号为止。 In most applications this is provided when the last (or only) QMainWindow is destroyed (see QApp.setQuitOnLastWindowClosed() ). 在大多数应用程序中,这是在销毁最后一个(或唯一一个) QMainWindow时提供的(请参阅QApp.setQuitOnLastWindowClosed() )。

Setting the Qt.Tool flag on a QMainWindow clears the WA_QuitOnClose flag meaning that closing the window will no longer signal to exit the application. QMainWindow上设置Qt.Tool标志将清除WA_QuitOnClose标志,这意味着关闭窗口将不再发出退出应用程序的信号。 However, you can turn this flag back on as described in this post on the Qt forum . 但是,您可以按照Qt论坛上的帖子中所述重新打开此标志。

window = QMainWindow(None, Qt.Tool)
window.show() # must be called before setting Qt::WA_QuitOnClose
window.setAttribute(Qt.WA_QuitOnClose)

Alternatively, you could also trigger the exit manually using QApp.quit() . 另外,您也可以使用QApp.quit()手动触发退出。

I found the solution, make your own quit button and connect the signal like below 我找到了解决方案,制作了自己的退出按钮并连接信号,如下所示

self.xbutton = QPushButton(self)
self.xbutton.setText("Quit")
self.xbutton.clicked.connect(self.close)

This destroy the app properly. 这会正确破坏应用程序。 Also instead of Qt.Tool use self.setWindowFlags(QtCore.Qt.FramelessWindowHint) Qt.Tool使用self.setWindowFlags(QtCore.Qt.FramelessWindowHint)代替self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM