Plumb QMessageBox::iconPixmap() to QPlatformMessageDialogHelper

For now this plumbs the QPixmap directly, but as a follow up we should
teach QMessageBox to deal with QIcons instead, and rejig the plumbing
correspondingly.

Change-Id: I51dee4240082abf0acb33b6ade553327295a99bd
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2022-10-26 20:12:00 +02:00
parent 91170aba0f
commit e5f777638b
4 changed files with 29 additions and 4 deletions

View File

@ -15,6 +15,7 @@
#include <QtCore/QUrl>
#include <QtCore/QVariant>
#include <QtGui/QColor>
#include <QtGui/QPixmap>
#include <algorithm>
@ -774,6 +775,7 @@ public:
QPlatformDialogHelper::StandardButtons buttons;
QList<QMessageDialogOptions::CustomButton> customButtons;
int nextCustomButtonId;
QPixmap iconPixmap;
};
QMessageDialogOptions::QMessageDialogOptions(QMessageDialogOptionsPrivate *dd)
@ -825,6 +827,16 @@ void QMessageDialogOptions::setIcon(Icon icon)
d->icon = icon;
}
void QMessageDialogOptions::setIconPixmap(const QPixmap &pixmap)
{
d->iconPixmap = pixmap;
}
QPixmap QMessageDialogOptions::iconPixmap() const
{
return d->iconPixmap;
}
QString QMessageDialogOptions::text() const
{
return d->text;

View File

@ -416,6 +416,9 @@ public:
void setIcon(Icon icon);
Icon icon() const;
void setIconPixmap(const QPixmap &pixmap);
QPixmap iconPixmap() const;
void setText(const QString &text);
QString text() const;

View File

@ -12,6 +12,7 @@
#include <QtGui/qtextdocument.h>
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/private/qcoregraphics_p.h>
#include <QtGui/qpa/qplatformtheme.h>
#include <AppKit/NSAlert.h>
@ -90,8 +91,18 @@ bool QCocoaMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality w
m_alert.messageText = text.toNSString();
m_alert.informativeText = toPlainText(options()->informativeText()).toNSString();
switch (options()->icon()) {
case QMessageDialogOptions::NoIcon:
switch (auto standardIcon = options()->icon()) {
case QMessageDialogOptions::NoIcon: {
// We only reflect the pixmap icon if the standard icon is unset,
// as setting a standard icon will also set a corresponding pixmap
// icon, which we don't want since it conflicts with the platform.
// If the user has set an explicit pixmap icon however, the standard
// icon will be NoIcon, so we're good.
QPixmap iconPixmap = options()->iconPixmap();
if (!iconPixmap.isNull())
m_alert.icon = [NSImage imageFromQImage:iconPixmap.toImage()];
break;
}
case QMessageDialogOptions::Information:
case QMessageDialogOptions::Question:
[m_alert setAlertStyle:NSAlertStyleInformational];
@ -104,8 +115,6 @@ bool QCocoaMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality w
break;
}
// FIXME: Propagate iconPixmap through dialog options
bool defaultButtonAdded = false;
bool cancelButtonAdded = false;

View File

@ -2719,6 +2719,7 @@ void QMessageBoxPrivate::helperPrepareShow(QPlatformDialogHelper *)
options->setDetailedText(q->detailedText());
#endif
options->setIcon(helperIcon(q->icon()));
options->setIconPixmap(q->iconPixmap());
options->setStandardButtons(helperStandardButtons(q));
}