Why QDialogButtonBox::Close could not emit closeEvent?
Review Request #130162 - Created June 19, 2017 and submitted
As Dr. Chaptian reported "do not show again" from system configuration problems dialog is not remembered, so I simply added
slotCloseto write the entry for K3b's KConfigGroup. but I have no idea why QDialogButtonBox::Close could not emit closeEvent, not need to connect, perhaps old Qt v4.x was able to work?
There are a bunch of things to note here.
Firstly, please always use the new Qt 5 by-reference connection syntax when adding new connections. The old string syntax
SLOT()are evaluated at runtime making them a substantial point of failure further down the road. https://wiki.qt.io/New_Signal_Slot_Syntax
Secondly, with QDialogButtonBox you want to connect the box, not the individual buttons, as seen earlier in the constructor.
Thirdly, your change is introducing excessive code. As Anthony said, closeEvent is called when the underlying QWidget gets closed.
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::close);without a new slot or anything would be sufficient to get exactly the same result as what you've done. That's not really the correct fix though...
Lastly, reading the QDialog documentation tells us why closeEvent is not called properly. The button
Closehas a reject role in the QDialogButtonBox meaning it will emit
rejectedwhich we connect to
QDialog::rejectwhich is documented as "Hides the modal dialog and sets the result code to Rejected.". Simply put QDialogs practically do not get closed, they get "hidden".
The way to fix this properly is to reimplemnt QDialog::done instead of the closeEvent.
doneis internally backing both accepted and rejected so it is always run. Change
done(int)and this should work without any other changes.