简体   繁体   English

PyQt5 QMessageBox 对话框从 QPushButton 但不是 QAction 关闭主窗口

[英]PyQt5 QMessageBox dialog closes main window from QPushButton but not QAction

Maybe a dumb question as I'm fairly new to PyQt but ...也许是一个愚蠢的问题,因为我对 PyQt 还很陌生,但是......

I've generated a main window (QMainWindow) dialog from QtDesigner and added in signal/slot connections我从 QtDesigner 生成了一个主窗口 (QMainWindow) 对话框并添加了信号/插槽连接

self.dlg_ui.quit_button.clicked.connect(self.quit)
self.dlg_ui.actionQuit.triggered.connect(self.quit)

Note: quit_button is a QPushButton object and actionQuit is a QAction object注意:quit_button 是一个 QPushButton 对象,actionQuit 是一个 QAction 对象

The slot self.quit opens a QtWidgets.QMessageBox.question dialog set up to offer file saving with a yes/no/cancel response but the dialog functions differently depending on where it was invoked from.插槽 self.quit 打开一个 QtWidgets.QMessageBox.question 对话框,该对话框设置为提供带有 yes/no/cancel 响应的文件保存,但该对话框的功能因调用它的位置而异。

If invoked by actionQuit.triggered the dialog does what it should but if invoked by quit_button.clicked the dialog immediately closes the main window.如果被 actionQuit.triggered 调用,对话框会做它应该做的事,但如果被 quit_button.clicked 调用,对话框会立即关闭主窗口。

Since they're both invoking the same code I'm at a loss to know why the QPushButton.clicked signal causes the main window to close.由于他们都在调用相同的代码,我不知道为什么 QPushButton.clicked 信号会导致主窗口关闭。

Any help in where to start looking would be greatly appreciated.任何关于从哪里开始寻找的帮助将不胜感激。

After a fair amount of work, first creating a simple version of the ui where everything worked and then slowly adding code from the broken ui, bit by bit, I've finally worked out what generated the problem.经过大量的工作,首先创建了一个简单的 ui 版本,其中一切正常,然后慢慢地从损坏的 ui 中添加代码,一点一点,我终于弄清楚了是什么导致了问题。

It turned out to be in the ui file created from QtDesigner - just about the last thing I added to the test script which, being automatically generated, I assumed the glitch was more likely to be in my own code.结果是在从 QtDesigner 创建的 ui 文件中 - 几乎是我添加到测试脚本的最后一件事,它是自动生成的,我认为故障更有可能出现在我自己的代码中。

In QtDesigner I'd added in connections between the button.clicked and action.triggered to MainWindow.close().在 QtDesigner 中,我将 button.clicked 和 action.triggered 之间的连接添加到 MainWindow.close()。 Once I deleted those links in QtDesigner and regenerated the ui file both the menu action and the button worked as they should.一旦我在 QtDesigner 中删除了这些链接并重新生成了 ui 文件,菜单操作和按钮都可以正常工作。

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

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