From 076eef86db02331bb4874a8f79306ec1aeb26ffd Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 25 Sep 2018 13:54:17 +0200 Subject: iOS: add support for custom buttons in native MessageDialog helper [ChangeLog][QtWidgets][QMessageBox] On Android and iOS it's now possible to show a QMessageBox with custom buttons as a native dialog. Task-number: QTBUG-35545 Change-Id: Id3be69e70468f767a43ea5f2ba64f9bac1898423 Reviewed-by: Shawn Rutledge Reviewed-by: Richard Moe Gustavsen --- src/plugins/platforms/ios/qiosmessagedialog.h | 1 + src/plugins/platforms/ios/qiosmessagedialog.mm | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'src/plugins/platforms/ios') diff --git a/src/plugins/platforms/ios/qiosmessagedialog.h b/src/plugins/platforms/ios/qiosmessagedialog.h index 92a4db8319..913fe0c2e9 100644 --- a/src/plugins/platforms/ios/qiosmessagedialog.h +++ b/src/plugins/platforms/ios/qiosmessagedialog.h @@ -63,6 +63,7 @@ private: UIAlertController *m_alertController; QString messageTextPlain(); UIAlertAction *createAction(StandardButton button); + UIAlertAction *createAction(const QMessageDialogOptions::CustomButton &customButton); }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/ios/qiosmessagedialog.mm b/src/plugins/platforms/ios/qiosmessagedialog.mm index a7de9b473a..254922701a 100644 --- a/src/plugins/platforms/ios/qiosmessagedialog.mm +++ b/src/plugins/platforms/ios/qiosmessagedialog.mm @@ -79,6 +79,18 @@ inline QString QIOSMessageDialog::messageTextPlain() return text; } +inline UIAlertAction *QIOSMessageDialog::createAction( + const QMessageDialogOptions::CustomButton &customButton) +{ + const QString label = QPlatformTheme::removeMnemonics(customButton.label); + const UIAlertActionStyle style = UIAlertActionStyleDefault; + + return [UIAlertAction actionWithTitle:label.toNSString() style:style handler:^(UIAlertAction *) { + hide(); + emit clicked(static_cast(customButton.id), customButton.role); + }]; +} + inline UIAlertAction *QIOSMessageDialog::createAction(StandardButton button) { const StandardButton labelButton = button == NoButton ? Ok : button; @@ -118,12 +130,18 @@ bool QIOSMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality win message:messageTextPlain().toNSString() preferredStyle:UIAlertControllerStyleAlert] retain]; + const QVector customButtons = options()->customButtons(); + for (const QMessageDialogOptions::CustomButton &button : customButtons) { + UIAlertAction *act = createAction(button); + [m_alertController addAction:act]; + } + if (StandardButtons buttons = options()->standardButtons()) { for (int i = FirstButton; i < LastButton; i<<=1) { if (i & buttons) [m_alertController addAction:createAction(StandardButton(i))]; } - } else { + } else if (customButtons.isEmpty()) { // We need at least one button to allow the user close the dialog [m_alertController addAction:createAction(NoButton)]; } -- cgit v1.2.3