简体   繁体   中英

PyQt5 QMessageBox dialog closes main window from QPushButton but not QAction

Maybe a dumb question as I'm fairly new to PyQt but ...

I've generated a main window (QMainWindow) dialog from QtDesigner and added in signal/slot connections

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

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.

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.

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.

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.

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.

In QtDesigner I'd added in connections between the button.clicked and action.triggered to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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