diff --git a/src/libs/utils/checkablemessagebox.cpp b/src/libs/utils/checkablemessagebox.cpp index b6bf66e2761..19d3669adac 100644 --- a/src/libs/utils/checkablemessagebox.cpp +++ b/src/libs/utils/checkablemessagebox.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -122,25 +123,38 @@ static void show(QWidget *parent, if (decider.shouldAskAgain && !decider.shouldAskAgain()) { if (callback) { QMetaObject::invokeMethod( - guard, [callback, acceptButton] { callback(acceptButton); }, Qt::QueuedConnection); + guard ? guard : qApp, + [callback, acceptButton] { callback(acceptButton); }, + Qt::QueuedConnection); } + return; } QMessageBox *msgBox = new QMessageBox(parent); prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox); - QObject::connect(msgBox, &QMessageBox::finished, guard, [msgBox, callback, decider, acceptButton] { - QMessageBox::StandardButton clickedBtn = msgBox->standardButton(msgBox->clickedButton()); + std::optional> guardPtr; + if (guard) + guardPtr = guard; - if (decider.doNotAskAgain && msgBox->checkBox()->isChecked() - && (acceptButton == QMessageBox::NoButton || clickedBtn == acceptButton)) - decider.doNotAskAgain(); - if (callback) - callback(clickedBtn); + QObject::connect(msgBox, + &QMessageBox::finished, + [guardPtr, msgBox, callback, decider, acceptButton] { + QMessageBox::StandardButton clickedBtn = msgBox->standardButton( + msgBox->clickedButton()); - msgBox->deleteLater(); - }); + if (decider.doNotAskAgain && msgBox->checkBox()->isChecked() + && (acceptButton == QMessageBox::NoButton + || clickedBtn == acceptButton)) { + decider.doNotAskAgain(); + } + + if (callback && (!guardPtr || *guardPtr)) + callback(clickedBtn); + + msgBox->deleteLater(); + }); msgBox->show(); } diff --git a/src/libs/utils/checkablemessagebox.h b/src/libs/utils/checkablemessagebox.h index 4245cf6e1ff..364860e39f1 100644 --- a/src/libs/utils/checkablemessagebox.h +++ b/src/libs/utils/checkablemessagebox.h @@ -60,7 +60,7 @@ public: const QString &text, const CheckableDecider &decider, QMessageBox::StandardButtons buttons = QMessageBox::Ok, - QMessageBox::StandardButton defaultButton = QMessageBox::NoButton, + QMessageBox::StandardButton defaultButton = QMessageBox::Ok, QMap buttonTextOverrides = {}, const QString &msg = {}); @@ -71,8 +71,8 @@ public: const CheckableDecider &decider, QObject *guard = nullptr, std::function callback = nullptr, - QMessageBox::StandardButtons buttons = QMessageBox::Ok, - QMessageBox::StandardButton defaultButton = QMessageBox::NoButton, + QMessageBox::StandardButtons buttons = QMessageBox::Ok, + QMessageBox::StandardButton defaultButton = QMessageBox::Ok, QMap buttonTextOverrides = {}, const QString &msg = {});