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 <QHBoxLayout>
#include <QLabel>
#include <QPointer>
#include <QPushButton>
#include <QStyle>
#include <QTextEdit>
@@ -122,21 +123,34 @@ 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<QPointer<QObject>> guardPtr;
if (guard)
guardPtr = guard;
QObject::connect(msgBox,
&QMessageBox::finished,
[guardPtr, msgBox, callback, decider, acceptButton] {
QMessageBox::StandardButton clickedBtn = msgBox->standardButton(
msgBox->clickedButton());
if (decider.doNotAskAgain && msgBox->checkBox()->isChecked()
&& (acceptButton == QMessageBox::NoButton || clickedBtn == acceptButton))
&& (acceptButton == QMessageBox::NoButton
|| clickedBtn == acceptButton)) {
decider.doNotAskAgain();
if (callback)
}
if (callback && (!guardPtr || *guardPtr))
callback(clickedBtn);
msgBox->deleteLater();

View File

@@ -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<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
const QString &msg = {});
@@ -72,7 +72,7 @@ public:
QObject *guard = nullptr,
std::function<void(QMessageBox::StandardButton choosenBtn)> callback = nullptr,
QMessageBox::StandardButtons buttons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton,
QMessageBox::StandardButton defaultButton = QMessageBox::Ok,
QMap<QMessageBox::StandardButton, QString> buttonTextOverrides = {},
const QString &msg = {});