Utils: Allow async CheckableMsgBox without guard

Change-Id: I76cb8dcb5aa89cc9b98410daeeeba668279c6e45
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-01-09 14:30:49 +01:00
parent 03bce22663
commit a14cd5d702
2 changed files with 27 additions and 13 deletions

View File

@@ -12,6 +12,7 @@
#include <QCheckBox> #include <QCheckBox>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QPointer>
#include <QPushButton> #include <QPushButton>
#include <QStyle> #include <QStyle>
#include <QTextEdit> #include <QTextEdit>
@@ -122,25 +123,38 @@ static void show(QWidget *parent,
if (decider.shouldAskAgain && !decider.shouldAskAgain()) { if (decider.shouldAskAgain && !decider.shouldAskAgain()) {
if (callback) { if (callback) {
QMetaObject::invokeMethod( QMetaObject::invokeMethod(
guard, [callback, acceptButton] { callback(acceptButton); }, Qt::QueuedConnection); guard ? guard : qApp,
[callback, acceptButton] { callback(acceptButton); },
Qt::QueuedConnection);
} }
return; return;
} }
QMessageBox *msgBox = new QMessageBox(parent); QMessageBox *msgBox = new QMessageBox(parent);
prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox); prepare(icon, title, text, decider, buttons, defaultButton, buttonTextOverrides, msg, *msgBox);
QObject::connect(msgBox, &QMessageBox::finished, guard, [msgBox, callback, decider, acceptButton] { std::optional<QPointer<QObject>> guardPtr;
QMessageBox::StandardButton clickedBtn = msgBox->standardButton(msgBox->clickedButton()); if (guard)
guardPtr = guard;
if (decider.doNotAskAgain && msgBox->checkBox()->isChecked() QObject::connect(msgBox,
&& (acceptButton == QMessageBox::NoButton || clickedBtn == acceptButton)) &QMessageBox::finished,
decider.doNotAskAgain(); [guardPtr, msgBox, callback, decider, acceptButton] {
if (callback) QMessageBox::StandardButton clickedBtn = msgBox->standardButton(
callback(clickedBtn); 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(); msgBox->show();
} }

View File

@@ -60,7 +60,7 @@ public:
const QString &text, const QString &text,
const CheckableDecider &decider, const CheckableDecider &decider,
QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButtons buttons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton, QMessageBox::StandardButton defaultButton = QMessageBox::Ok,
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {}, QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
const QString &msg = {}); const QString &msg = {});
@@ -71,8 +71,8 @@ public:
const CheckableDecider &decider, const CheckableDecider &decider,
QObject *guard = nullptr, QObject *guard = nullptr,
std::function<void(QMessageBox::StandardButton choosenBtn)> callback = nullptr, std::function<void(QMessageBox::StandardButton choosenBtn)> callback = nullptr,
QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButtons buttons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton, QMessageBox::StandardButton defaultButton = QMessageBox::Ok,
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {}, QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
const QString &msg = {}); const QString &msg = {});