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 <shawn.rutledge@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Ulf Hermann 2018-09-25 13:54:17 +02:00 committed by Shawn Rutledge
parent 9a8175a131
commit 076eef86db
2 changed files with 20 additions and 1 deletions

View File

@ -63,6 +63,7 @@ private:
UIAlertController *m_alertController;
QString messageTextPlain();
UIAlertAction *createAction(StandardButton button);
UIAlertAction *createAction(const QMessageDialogOptions::CustomButton &customButton);
};
QT_END_NAMESPACE

View File

@ -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<QPlatformDialogHelper::StandardButton>(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<QMessageDialogOptions::CustomButton> 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)];
}