Allow disable native messagebox dialog

The native style may not match the program style and it
makes sense to give an option rather than forcing native style.

Before it could only be done with setting AA_DontUseNativeDialogs
on the app, but it is reasonable to use native file dialogs and
Qt styled messageboxes - and it is extremely cumbersome - especially
since messageboxes often can start save dialogs.

It can look a bit strange that these introduced options has just one
option (DontUseNativeDialog) and four functions, but this way it works
similar to QColorDialog, QFileDialog and QFontDialog.

[ChangeLog][QWidgets][QMessageBox] Added options functionality to
QMessagebox. The currently only option available is DontUseNativeDialog.

Change-Id: I70282fcfaa66f245f7e679b8897c607bcaff333f
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Thorbjørn Lund Martsum 2023-04-21 15:13:19 +02:00
parent 642f799fc6
commit 29b2506e8c
5 changed files with 163 additions and 2 deletions

View File

@ -778,6 +778,7 @@ public:
QPixmap iconPixmap;
QString checkBoxLabel;
Qt::CheckState checkBoxState = Qt::Unchecked;
QMessageDialogOptions::Options options;
};
QMessageDialogOptions::QMessageDialogOptions(QMessageDialogOptionsPrivate *dd)
@ -924,6 +925,29 @@ Qt::CheckState QMessageDialogOptions::checkBoxState() const
return d->checkBoxState;
}
void QMessageDialogOptions::setOption(QMessageDialogOptions::Option option, bool on)
{
if (!(d->options & option) != !on)
setOptions(d->options ^ option);
}
bool QMessageDialogOptions::testOption(QMessageDialogOptions::Option option) const
{
return d->options & option;
}
void QMessageDialogOptions::setOptions(QMessageDialogOptions::Options options)
{
if (options != d->options)
d->options = options;
}
QMessageDialogOptions::Options QMessageDialogOptions::options() const
{
return d->options;
}
QPlatformDialogHelper::ButtonRole QPlatformDialogHelper::buttonRole(QPlatformDialogHelper::StandardButton button)
{
switch (button) {

View File

@ -403,6 +403,11 @@ protected:
~QMessageDialogOptions();
public:
// Keep in sync with QMessageBox Option
enum class Option : quint8 { DontUseNativeDialog = 0x00000001 };
Q_DECLARE_FLAGS(Options, Option);
Q_FLAG(Options);
// Keep in sync with QMessageBox::Icon
enum StandardIcon { NoIcon, Information, Warning, Critical, Question };
Q_ENUM(StandardIcon)
@ -428,6 +433,11 @@ public:
void setDetailedText(const QString &text);
QString detailedText() const;
void setOption(Option option, bool on = true);
bool testOption(Option option) const;
void setOptions(Options options);
Options options() const;
void setStandardButtons(QPlatformDialogHelper::StandardButtons buttons);
QPlatformDialogHelper::StandardButtons standardButtons() const;

View File

@ -189,6 +189,7 @@ public:
void retranslateStrings();
void setVisible(bool visible) override;
bool canBeNativeDialog() const override;
static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon,
const QString &title, const QString &text,
@ -769,6 +770,12 @@ void QMessageBoxPrivate::_q_clicked(QPlatformDialogHelper::StandardButton button
\sa ButtonRole, standardButtons
*/
/*!
\enum QMessageBox::Option
\since 6.6
\value DontUseNativeDialog Don't use the native message dialog.
*/
/*!
\fn void QMessageBox::buttonClicked(QAbstractButton *button)
@ -1224,6 +1231,78 @@ QCheckBox* QMessageBox::checkBox() const
return d->checkbox;
}
/*!
\since 6.6
Sets the given \a option to be enabled if \a on is true; otherwise,
clears the given \a option.
Options (particularly the DontUseNativeDialogs option) should be set
before showing the dialog.
Setting options while the dialog is visible is not guaranteed to have
an immediate effect on the dialog.
Setting options after changing other properties may cause these
values to have no effect.
\sa options, testOption()
*/
void QMessageBox::setOption(QMessageBox::Option option, bool on)
{
const QMessageBox::Options previousOptions = options();
if (!(previousOptions & option) != !on)
setOptions(previousOptions ^ option);
}
/*!
\since 6.6
Returns \c true if the given \a option is enabled; otherwise, returns
false.
\sa options, setOption()
*/
bool QMessageBox::testOption(QMessageBox::Option option) const
{
Q_D(const QMessageBox);
return d->options->testOption(static_cast<QMessageDialogOptions::Option>(option));
}
void QMessageBox::setOptions(QMessageBox::Options options)
{
Q_D(QMessageBox);
if (QMessageBox::options() == options)
return;
d->options->setOptions(QMessageDialogOptions::Option(int(options)));
}
QMessageBox::Options QMessageBox::options() const
{
Q_D(const QMessageBox);
return QMessageBox::Options(int(d->options->options()));
}
/*!
\property QMessageBox::options
\brief options that affect the look and feel of the dialog
\since 6.6.
By default, these options are disabled.
The option DontUseNativeDialog should be set
before changing dialog properties or showing the dialog.
Setting options while the dialog is visible is not guaranteed to have
an immediate effect on the dialog.
Setting options after changing other properties may cause these
values to have no effect.
\sa setOption(), testOption()
*/
/*!
\property QMessageBox::text
\brief the message box text to be displayed.
@ -2717,6 +2796,25 @@ static QPlatformDialogHelper::StandardButtons helperStandardButtons(QMessageBox
return buttons;
}
bool QMessageBoxPrivate::canBeNativeDialog() const
{
// Don't use Q_Q here! This function is called from ~QDialog,
// so Q_Q calling q_func() invokes undefined behavior (invalid cast in q_func()).
const QDialog * const q = static_cast<const QMessageBox*>(q_ptr);
if (nativeDialogInUse)
return true;
if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
|| q->testAttribute(Qt::WA_DontShowOnScreen)
|| (options->options() & QMessageDialogOptions::Option::DontUseNativeDialog)) {
return false;
}
if (strcmp(QMessageBox::staticMetaObject.className(), q->metaObject()->className()) != 0)
return false;
return QDialogPrivate::canBeNativeDialog();
}
void QMessageBoxPrivate::helperPrepareShow(QPlatformDialogHelper *)
{
Q_Q(QMessageBox);

View File

@ -31,8 +31,14 @@ class Q_WIDGETS_EXPORT QMessageBox : public QDialog
Q_PROPERTY(QString informativeText READ informativeText WRITE setInformativeText)
Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags
WRITE setTextInteractionFlags)
Q_PROPERTY(Options options READ options WRITE setOptions)
public:
// Keep in sync with MessageBoxOption in qplatformdialoghelper.h
enum class Option : quint8 {
DontUseNativeDialog = 0x00000001
};
Q_FLAG(Option)
enum Icon {
// keep this in sync with QMessageDialogOptions::StandardIcon
NoIcon = 0,
@ -95,8 +101,9 @@ public:
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
typedef StandardButton Button;
#endif
Q_DECLARE_FLAGS(Options, Option)
Q_DECLARE_FLAGS(StandardButtons, StandardButton)
Q_FLAG(StandardButtons)
explicit QMessageBox(QWidget *parent = nullptr);
@ -149,6 +156,11 @@ public:
void setCheckBox(QCheckBox *cb);
QCheckBox* checkBox() const;
void setOption(Option option, bool on = true);
bool testOption(Option option) const;
void setOptions(Options options);
Options options() const;
static StandardButton information(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);

View File

@ -38,6 +38,9 @@ private slots:
void detailsButtonText();
void expandDetailsWithoutMoving();
void optionsEmptyByDefault();
void changeNativeOption();
#ifndef Q_OS_MAC
void shortcut();
#endif
@ -584,6 +587,20 @@ void tst_QMessageBox::expandDetailsWithoutMoving() // QTBUG-32473
QCOMPARE(box.geometry().topLeft(), geom.topLeft());
}
void tst_QMessageBox::optionsEmptyByDefault()
{
QMessageBox b;
QCOMPARE(b.options(), QMessageBox::Options());
QVERIFY(!b.testOption(QMessageBox::Option::DontUseNativeDialog));
}
void tst_QMessageBox::changeNativeOption()
{
QMessageBox b;
b.setOption(QMessageBox::Option::DontUseNativeDialog);
QCOMPARE(b.options(), QMessageBox::Options(QMessageBox::Option::DontUseNativeDialog));
}
void tst_QMessageBox::incorrectDefaultButton()
{
ExecCloseHelper closeHelper;