5#include <QtWidgets/qmessagebox.h>
7#include <QtWidgets/qdialogbuttonbox.h>
8#include "private/qlabel_p.h"
9#include "private/qapplication_p.h"
10#include <QtCore/qlist.h>
11#include <QtCore/qdebug.h>
12#include <QtWidgets/qstyle.h>
13#include <QtWidgets/qstyleoption.h>
14#include <QtWidgets/qgridlayout.h>
15#include <QtWidgets/qpushbutton.h>
16#include <QtWidgets/qcheckbox.h>
17#include <QtGui/qaccessible.h>
18#include <QtGui/qicon.h>
19#include <QtGui/qtextdocument.h>
20#include <QtWidgets/qapplication.h>
21#if QT_CONFIG(textedit)
22#include <QtWidgets/qtextedit.h>
25#include <QtWidgets/qmenu.h>
28#include <QtGui/qfont.h>
29#include <QtGui/qfontmetrics.h>
30#include <QtGui/qclipboard.h>
31#include "private/qabstractbutton_p.h"
32#include <QtGui/qpa/qplatformtheme.h>
34#include <QtCore/qanystringview.h>
35#include <QtCore/qdebug.h>
36#include <QtCore/qpointer.h>
37#include <QtCore/qversionnumber.h>
40# include <QtCore/qt_windows.h>
41#include <qpa/qplatformnativeinterface.h>
48using namespace Qt::StringLiterals;
51HMENU qt_getWindowsSystemMenu(
const QWidget *w)
53 if (QWindow *window = QApplicationPrivate::windowForWidget(w))
54 if (
void *handle = QGuiApplication::platformNativeInterface()->nativeResourceForWindow(
"handle", window))
55 return GetSystemMenu(
reinterpret_cast<HWND>(handle),
false);
60static_assert(qToUnderlying(QMessageBox::ButtonRole::NRoles) ==
61 qToUnderlying(QDialogButtonBox::ButtonRole::NRoles),
62 "QMessageBox::ButtonRole and QDialogButtonBox::ButtonRole out of sync!");
64static_assert(std::is_same_v<std::underlying_type_t<QMessageBox::ButtonRole>,
65 std::underlying_type_t<QDialogButtonBox::ButtonRole>>);
70static_assert(
static_cast<
int>(QMessageBox::StandardButton::LastButton) ==
71 static_cast<
int>(QDialogButtonBox::StandardButton::LastButton),
72 "QMessageBox::StandardButton and QDialogButtonBox::StandardButton out of sync!");
79#if QT_CONFIG(textedit)
80class QMessageBoxDetailsText :
public QWidget
84 class TextEdit :
public QTextEdit
87 TextEdit(QWidget *parent=
nullptr) : QTextEdit(parent) { }
88#ifndef QT_NO_CONTEXTMENU
89 void contextMenuEvent(QContextMenuEvent * e) override
91 if (QMenu *menu = createStandardContextMenu()) {
92 menu->setAttribute(Qt::WA_DeleteOnClose);
93 menu->popup(e->globalPos());
99 QMessageBoxDetailsText(QWidget *parent=
nullptr)
101 , copyAvailable(
false)
103 QVBoxLayout *layout =
new QVBoxLayout;
104 layout->setContentsMargins(QMargins());
105 QFrame *line =
new QFrame(
this);
106 line->setFrameShape(QFrame::HLine);
107 line->setFrameShadow(QFrame::Sunken);
108 layout->addWidget(line);
109 textEdit =
new TextEdit();
110 textEdit->setFixedHeight(100);
111 textEdit->setFocusPolicy(Qt::NoFocus);
112 textEdit->setReadOnly(
true);
113 layout->addWidget(textEdit);
116 connect(textEdit, &TextEdit::copyAvailable,
117 this, &QMessageBoxDetailsText::textCopyAvailable);
119 void setText(
const QString &text) { textEdit->setPlainText(text); }
120 QString text()
const {
return textEdit->toPlainText(); }
124#ifdef QT_NO_CLIPBOARD
136 textEdit->selectAll();
140 void textCopyAvailable(
bool available)
142 copyAvailable = available;
156#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU_ONLY >= 1600
&& Q_CC_GNU_ONLY < 1700
&& !defined(__OPTIMIZE__)
189 Q_DECLARE_PUBLIC(QMessageBox)
193#if QT_CONFIG(textedit)
203 void helperClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role);
221 const QString &title,
const QString &text,
222 int button0,
int button1,
int button2);
224 const QString &title,
const QString &text,
225 const QString &button0Text,
226 const QString &button1Text,
227 const QString &button2Text,
228 int defaultButtonNumber,
229 int escapeButtonNumber);
248#if QT_CONFIG(textedit)
271 label->setObjectName(
"qt_msgbox_label"_L1);
272 label->setTextInteractionFlags(Qt::TextInteractionFlags(q->style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr, q)));
273 label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
274 label->setOpenExternalLinks(
true);
275 iconLabel =
new QLabel(q);
276 iconLabel->setObjectName(
"qt_msgboxex_icon_label"_L1);
277 iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
279 buttonBox =
new QDialogButtonBox;
280 buttonBox->setObjectName(
"qt_msgbox_buttonbox"_L1);
281 buttonBox->setCenterButtons(q->style()->styleHint(QStyle::SH_MessageBox_CenterButtons,
nullptr, q));
282 QObjectPrivate::connect(buttonBox, &QDialogButtonBox::clicked,
283 this, &QMessageBoxPrivate::buttonClicked);
285 if (!title.isEmpty() || !text.isEmpty()) {
286 q->setWindowTitle(title);
295 icon = QMessageBox::NoIcon;
302 QGridLayout *grid =
new QGridLayout;
303 const bool hasIcon = !iconLabel->pixmap().isNull();
306 grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop);
307 iconLabel->setVisible(hasIcon);
309 QSpacerItem *indentSpacer =
new QSpacerItem(14, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
311 QSpacerItem *indentSpacer =
new QSpacerItem(hasIcon ? 7 : 15, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
313 grid->addItem(indentSpacer, 0, hasIcon ? 1 : 0, 2, 1);
314 grid->addWidget(label, 0, hasIcon ? 2 : 1, 1, 1);
315 if (informativeLabel) {
317 informativeLabel->setContentsMargins(0, 7, 0, 7);
319 grid->addWidget(informativeLabel, 1, hasIcon ? 2 : 1, 1, 1);
322 grid->addWidget(checkbox, informativeLabel ? 2 : 1, hasIcon ? 2 : 1, 1, 1, Qt::AlignLeft);
324 grid->addItem(
new QSpacerItem(1, 15, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0);
326 grid->addItem(
new QSpacerItem(1, 7, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0);
330 grid->addWidget(buttonBox, grid->rowCount(), hasIcon ? 2 : 1, 1, 1);
331 grid->setContentsMargins(0, 0, 0, 0);
332 grid->setVerticalSpacing(8);
333 grid->setHorizontalSpacing(0);
334 q->setContentsMargins(24, 15, 24, 20);
335 grid->setRowStretch(1, 100);
336 grid->setRowMinimumHeight(2, 6);
338 grid->addWidget(buttonBox, grid->rowCount(), 0, 1, grid->columnCount());
340#if QT_CONFIG(textedit)
342 grid->addWidget(detailsText, grid->rowCount(), 0, 1, grid->columnCount());
344 grid->setSizeConstraint(QLayout::SetNoConstraint);
354 return layout->totalMinimumSize().width();
364 const QSize screenSize = q->screen()->availableGeometry().size();
365 int hardLimit = qMin(screenSize.width() - 480, 1000);
367 if (screenSize.width() <= 1024)
368 hardLimit = screenSize.width();
370 int softLimit = qMin(screenSize.width()/2, 420);
373 int softLimit = qMin(screenSize.width()/2, 500);
376 if (informativeLabel)
377 informativeLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
379 label->setWordWrap(
false);
382 if (width > softLimit) {
383 label->setWordWrap(
true);
386 if (width > hardLimit) {
387 label->d_func()->ensureTextControl();
388 if (QWidgetTextControl *control = label->d_func()->control) {
389 QTextOption opt = control->document()->defaultTextOption();
390 opt.setWrapMode(QTextOption::WrapAnywhere);
391 control->document()->setDefaultTextOption(opt);
397 if (informativeLabel) {
398 label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
399 QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Preferred);
400 policy.setHeightForWidth(
true);
401 informativeLabel->setSizePolicy(policy);
403 if (width > hardLimit) {
404 informativeLabel->d_func()->ensureTextControl();
405 if (QWidgetTextControl *control = informativeLabel->d_func()->control) {
406 QTextOption opt = control->document()->defaultTextOption();
407 opt.setWrapMode(QTextOption::WrapAnywhere);
408 control->document()->setDefaultTextOption(opt);
412 policy.setHeightForWidth(label->wordWrap());
413 label->setSizePolicy(policy);
416 QFontMetrics fm(QApplication::font(
"QMdiSubWindowTitleBar"));
417 int windowTitleWidth = qMin(fm.horizontalAdvance(q->windowTitle()) + 50, hardLimit);
418 if (windowTitleWidth > width)
419 width = windowTitleWidth;
422 int height = (layout->hasHeightForWidth())
423 ? layout->totalHeightForWidth(width)
424 : layout->totalMinimumSize().height();
426 q->setFixedSize(width, height);
427 QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);
432 switch (button & QMessageBox::ButtonMask) {
433 case QMessageBox::Ok:
435 case QMessageBox::Cancel:
437 case QMessageBox::Yes:
439 case QMessageBox::No:
441 case QMessageBox::Abort:
443 case QMessageBox::Retry:
445 case QMessageBox::Ignore:
447 case QMessageBox::YesToAll:
449 case QMessageBox::NoToAll:
458 if (
int standardButton = buttonBox->standardButton(button)) {
465 return standardButton;
471 auto customButtonIndex = customButtonList.indexOf(button);
472 if (customButtonIndex >= 0)
473 return QDialog::DialogCode::Accepted + customButtonIndex + 1;
475 return customButtonIndex;
481 Q_Q(
const QMessageBox);
483 if (rescode <= QDialog::Accepted) {
485 }
else if (clickedButton) {
486 switch (q->buttonRole(clickedButton)) {
487 case QMessageBox::AcceptRole:
488 case QMessageBox::YesRole:
489 return QDialog::Accepted;
490 case QMessageBox::RejectRole:
491 case QMessageBox::NoRole:
492 return QDialog::Rejected;
498 return QDialogPrivate::dialogCode();
504#if QT_CONFIG(textedit)
505 if (detailsButton && detailsText && button == detailsButton) {
506 detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel);
507 detailsText->setHidden(!detailsText->isHidden());
512 setClickedButton(button);
514 if (receiverToDisconnectOnClose) {
515 QObject::disconnect(q, signalToDisconnectOnClose, receiverToDisconnectOnClose,
516 memberToDisconnectOnClose);
517 receiverToDisconnectOnClose =
nullptr;
519 signalToDisconnectOnClose.clear();
520 memberToDisconnectOnClose.clear();
528 clickedButton = button;
529 emit q->buttonClicked(clickedButton);
531 auto resultCode = execReturnCode(button);
542 QAbstractButton *dialogButton = helperButton > QPlatformDialogHelper::LastButton ?
543 static_cast<QAbstractButton *>(options->customButton(helperButton)->button) :
544 q->button(QMessageBox::StandardButton(helperButton));
546 Q_ASSERT(dialogButton);
551 dialogButton->click();
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
771
772
773
774
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
822
823
824
825
828
829
830
831
832
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851QMessageBox::QMessageBox(QWidget *parent)
852 : QDialog(*
new QMessageBoxPrivate, parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876QMessageBox::QMessageBox(Icon icon,
const QString &title,
const QString &text,
877 StandardButtons buttons, QWidget *parent,
879: QDialog(*
new QMessageBoxPrivate, parent, f | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
882 d->init(title, text);
884 if (buttons != NoButton)
885 setStandardButtons(buttons);
889
890
891QMessageBox::~QMessageBox()
896
897
898
899
900
901void QMessageBox::addButton(QAbstractButton *button, ButtonRole role)
906 removeButton(button);
908 if (button->text().isEmpty()) {
909 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
910 if (
auto standardButton = QMessageBoxPrivate::standardButtonForRole(role))
911 button->setText(platformTheme->standardButtonText(standardButton));
914 if (button->text().isEmpty()) {
915 qWarning() <<
"Cannot add" << button <<
"without title";
920 d->buttonBox->addButton(button, (QDialogButtonBox::ButtonRole)role);
921 d->customButtonList.append(button);
922 d->autoAddOkButton =
false;
928 case QMessageBox::AcceptRole:
return QMessageBox::Ok;
929 case QMessageBox::RejectRole:
return QMessageBox::Cancel;
930 case QMessageBox::DestructiveRole:
return QMessageBox::Discard;
931 case QMessageBox::HelpRole:
return QMessageBox::Help;
932 case QMessageBox::ApplyRole:
return QMessageBox::Apply;
933 case QMessageBox::YesRole:
return QMessageBox::Yes;
934 case QMessageBox::NoRole:
return QMessageBox::No;
935 case QMessageBox::ResetRole:
return QMessageBox::Reset;
936 default:
return QMessageBox::NoButton;
941
942
943
944
945
946QPushButton *QMessageBox::addButton(
const QString& text, ButtonRole role)
949 QPushButton *pushButton =
new QPushButton(text);
950 addButton(pushButton, role);
956
957
958
959
960
961
962
963QPushButton *QMessageBox::addButton(StandardButton button)
966 QPushButton *pushButton = d->buttonBox->addButton((QDialogButtonBox::StandardButton)button);
968 d->autoAddOkButton =
false;
973
974
975
976
977void QMessageBox::removeButton(QAbstractButton *button)
980 d->customButtonList.removeAll(button);
981 if (d->escapeButton == button)
982 d->escapeButton =
nullptr;
983 if (d->defaultButton == button)
984 d->defaultButton =
nullptr;
985 d->buttonBox->removeButton(button);
990
991
992
993
994
995
996
997
998
999void QMessageBox::setStandardButtons(StandardButtons buttons)
1002 d->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(
int(buttons)));
1004 QList<QAbstractButton *> buttonList = d->buttonBox->buttons();
1005 if (!buttonList.contains(d->escapeButton))
1006 d->escapeButton =
nullptr;
1007 if (!buttonList.contains(d->defaultButton))
1008 d->defaultButton =
nullptr;
1009 d->autoAddOkButton =
false;
1013QMessageBox::StandardButtons QMessageBox::standardButtons()
const
1015 Q_D(
const QMessageBox);
1016 return QMessageBox::StandardButtons(
int(d->buttonBox->standardButtons()));
1020
1021
1022
1023
1024
1025QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button)
const
1027 Q_D(
const QMessageBox);
1028 return (QMessageBox::StandardButton)d->buttonBox->standardButton(button);
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043QAbstractButton *QMessageBox::button(StandardButton which)
const
1045 Q_D(
const QMessageBox);
1046 return d->buttonBox->button(QDialogButtonBox::StandardButton(which));
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067QAbstractButton *QMessageBox::escapeButton()
const
1069 Q_D(
const QMessageBox);
1070 return d->escapeButton;
1074
1075
1076
1077
1078
1079void QMessageBox::setEscapeButton(QAbstractButton *button)
1082 if (d->buttonBox->buttons().contains(button))
1083 d->escapeButton = button;
1087
1088
1089
1090
1091
1092void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)
1095 setEscapeButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
1101 detectedEscapeButton = escapeButton;
1106 detectedEscapeButton = buttonBox->button(QDialogButtonBox::Cancel);
1107 if (detectedEscapeButton)
1111 const QList<QAbstractButton *> buttons = buttonBox->buttons();
1112 if (buttons.size() == 1) {
1113 detectedEscapeButton = buttons.first();
1122 detectedEscapeButton = buttons.at(1 - idx);
1128 for (
auto *button : buttons) {
1129 if (buttonBox->buttonRole(button) == QDialogButtonBox::RejectRole) {
1130 if (detectedEscapeButton) {
1131 detectedEscapeButton =
nullptr;
1134 detectedEscapeButton = button;
1137 if (detectedEscapeButton)
1141 for (
auto *button : buttons) {
1142 if (buttonBox->buttonRole(button) == QDialogButtonBox::NoRole) {
1143 if (detectedEscapeButton) {
1144 detectedEscapeButton =
nullptr;
1147 detectedEscapeButton = button;
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165QAbstractButton *QMessageBox::clickedButton()
const
1167 Q_D(
const QMessageBox);
1168 return d->clickedButton;
1172
1173
1174
1175
1176
1177
1178QPushButton *QMessageBox::defaultButton()
const
1180 Q_D(
const QMessageBox);
1181 return d->defaultButton;
1185
1186
1187
1188
1189
1190void QMessageBox::setDefaultButton(QPushButton *button)
1193 if (!d->buttonBox->buttons().contains(button))
1195 d->defaultButton = button;
1196 button->setDefault(
true);
1201
1202
1203
1204
1205
1206void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)
1209 setDefaultButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
1213
1214
1215
1216
1217
1218
1220void QMessageBox::setCheckBox(QCheckBox *cb)
1224 if (cb == d->checkbox)
1228 d->checkbox->hide();
1229 layout()->removeWidget(d->checkbox);
1230 if (d->checkbox->parentWidget() ==
this) {
1231 d->checkbox->setParent(
nullptr);
1232 d->checkbox->deleteLater();
1237 QSizePolicy sp = d->checkbox->sizePolicy();
1238 sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
1239 d->checkbox->setSizePolicy(sp);
1246
1247
1248
1249
1251QCheckBox* QMessageBox::checkBox()
const
1253 Q_D(
const QMessageBox);
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273void QMessageBox::setOption(QMessageBox::Option option,
bool on)
1276 d->optionsExplicitlySet =
true;
1278 const QMessageBox::Options previousOptions = options();
1279 if (!(previousOptions & option) != !on)
1280 setOptions(previousOptions ^ option);
1284
1285
1286
1287
1288
1289
1290
1291bool QMessageBox::testOption(QMessageBox::Option option)
const
1293 Q_D(
const QMessageBox);
1294 return d->options->testOption(
static_cast<QMessageDialogOptions::Option>(option));
1297void QMessageBox::setOptions(QMessageBox::Options options)
1300 d->optionsExplicitlySet =
true;
1302 if (QMessageBox::options() == options)
1304 d->options->setOptions(QMessageDialogOptions::Option(
int(options)));
1307QMessageBox::Options QMessageBox::options()
const
1309 Q_D(
const QMessageBox);
1310 return QMessageBox::Options(
int(d->options->options()));
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348QString QMessageBox::text()
const
1350 Q_D(
const QMessageBox);
1351 return d->label->text();
1354void QMessageBox::setText(
const QString &text)
1357 d->label->setText(text);
1358 d->label->setWordWrap(d->label->textFormat() == Qt::RichText
1359 || (d->label->textFormat() == Qt::AutoText && Qt::mightBeRichText(text)));
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408QMessageBox::Icon QMessageBox::icon()
const
1410 Q_D(
const QMessageBox);
1414void QMessageBox::setIcon(Icon icon)
1417 setIconPixmap(QMessageBoxPrivate::standardIcon((QMessageBox::Icon)icon,
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434QPixmap QMessageBox::iconPixmap()
const
1436 Q_D(
const QMessageBox);
1437 return d->iconLabel->pixmap();
1440void QMessageBox::setIconPixmap(
const QPixmap &pixmap)
1443 d->iconLabel->setPixmap(pixmap);
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459Qt::TextFormat QMessageBox::textFormat()
const
1461 Q_D(
const QMessageBox);
1462 return d->label->textFormat();
1465void QMessageBox::setTextFormat(Qt::TextFormat format)
1468 d->label->setTextFormat(format);
1469 d->label->setWordWrap(format == Qt::RichText
1470 || (format == Qt::AutoText && Qt::mightBeRichText(d->label->text())));
1471 if (d->informativeLabel)
1472 d->informativeLabel->setTextFormat(format);
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1488Qt::TextInteractionFlags QMessageBox::textInteractionFlags()
const
1490 Q_D(
const QMessageBox);
1491 return d->label->textInteractionFlags();
1494void QMessageBox::setTextInteractionFlags(Qt::TextInteractionFlags flags)
1497 d->label->setTextInteractionFlags(flags);
1501
1502
1503bool QMessageBox::event(QEvent *e)
1506 bool result = QDialog::event(e);
1507 switch (e->type()) {
1508 case QEvent::LayoutRequest:
1509 d_func()->updateSize();
1511 case QEvent::LanguageChange:
1512 d_func()->retranslateStrings();
1514 case QEvent::Polish:
1515 if (!d->optionsExplicitlySet && metaObject() != &QMessageBox::staticMetaObject)
1516 setOption(QMessageBox::Option::DontUseNativeDialog);
1525
1526
1527void QMessageBox::resizeEvent(QResizeEvent *event)
1529 QDialog::resizeEvent(event);
1533
1534
1535void QMessageBox::closeEvent(QCloseEvent *e)
1538 if (!d->detectedEscapeButton) {
1542 QDialog::closeEvent(e);
1543 if (!d->clickedButton) {
1544 d->clickedButton = d->detectedEscapeButton;
1545 setResult(d->execReturnCode(d->detectedEscapeButton));
1550
1551
1552void QMessageBox::changeEvent(QEvent *ev)
1555 switch (ev->type()) {
1556 case QEvent::StyleChange:
1558 if (d->icon != NoIcon)
1560 Qt::TextInteractionFlags flags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this));
1561 d->label->setTextInteractionFlags(flags);
1562 d->buttonBox->setCenterButtons(style()->styleHint(QStyle::SH_MessageBox_CenterButtons,
nullptr,
this));
1563 if (d->informativeLabel)
1564 d->informativeLabel->setTextInteractionFlags(flags);
1567 case QEvent::FontChange:
1568 case QEvent::ApplicationFontChange:
1573 d->label->setFont(f);
1580 QDialog::changeEvent(ev);
1584
1585
1586void QMessageBox::keyPressEvent(QKeyEvent *e)
1588#if QT_CONFIG(shortcut)
1590 if (e->matches(QKeySequence::Cancel)) {
1591 if (d->detectedEscapeButton) {
1593 d->detectedEscapeButton->animateClick();
1595 d->detectedEscapeButton->click();
1602#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
1604#if QT_CONFIG(textedit)
1605 if (e == QKeySequence::Copy) {
1606 if (d->detailsText && d->detailsText->isVisible() && d->detailsText->copy()) {
1607 e->setAccepted(
true);
1610 }
else if (e == QKeySequence::SelectAll && d->detailsText && d->detailsText->isVisible()) {
1611 d->detailsText->selectAll();
1612 e->setAccepted(
true);
1617#if defined(Q_OS_WIN)
1618 if (e == QKeySequence::Copy) {
1619 const auto separator =
"---------------------------\n"_L1;
1621 textToCopy += separator + windowTitle() + u'\n' + separator
1622 + d->label->text() + u'\n' + separator;
1624 if (d->informativeLabel)
1625 textToCopy += d->informativeLabel->text() + u'\n' + separator;
1627 const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
1628 for (
const auto *button : buttons)
1629 textToCopy += button->text() +
" "_L1;
1630 textToCopy += u'\n' + separator;
1631#if QT_CONFIG(textedit)
1633 textToCopy += d->detailsText->text() + u'\n' + separator;
1635 QGuiApplication::clipboard()->setText(textToCopy);
1642#ifndef QT_NO_SHORTCUT
1643 if (!(e->modifiers() & (Qt::AltModifier | Qt::ControlModifier | Qt::MetaModifier))) {
1644 int key = e->key() & ~Qt::MODIFIER_MASK;
1646 const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
1647 for (
auto *pb : buttons) {
1648 QKeySequence shortcut = pb->shortcut();
1649 if (!shortcut.isEmpty() && key == shortcut[0].key()) {
1657 QDialog::keyPressEvent(e);
1661
1662
1663
1664
1665
1666
1667
1668void QMessageBox::open(QObject *receiver,
const char *member)
1671 const char *signal = member && strchr(member,
'*') ? SIGNAL(buttonClicked(QAbstractButton*))
1672 : SIGNAL(finished(
int));
1673 connect(
this, signal, receiver, member);
1674 d->signalToDisconnectOnClose = signal;
1675 d->receiverToDisconnectOnClose = receiver;
1676 d->memberToDisconnectOnClose = member;
1685 if (autoAddOkButton)
1686 q->addButton(QMessageBox::Ok);
1693 setNativeDialogVisible(visible);
1698 static_cast<QWidget*>(q_ptr)->setAttribute(Qt::WA_DontShowOnScreen, nativeDialogInUse);
1700 QDialogPrivate::setVisible(visible);
1704
1705
1706
1707
1708QList<QAbstractButton *> QMessageBox::buttons()
const
1710 Q_D(
const QMessageBox);
1711 return d->buttonBox->buttons();
1715
1716
1717
1718
1719
1720QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button)
const
1722 Q_D(
const QMessageBox);
1723 return QMessageBox::ButtonRole(d->buttonBox->buttonRole(button));
1727
1728
1729void QMessageBox::showEvent(QShowEvent *e)
1732 d->clickedButton =
nullptr;
1735#if QT_CONFIG(accessibility)
1736 QAccessibleEvent event(
this, QAccessible::Alert);
1737 QAccessible::updateAccessibility(&event);
1739#if defined(Q_OS_WIN)
1740 if (
const HMENU systemMenu = qt_getWindowsSystemMenu(
this)) {
1741 EnableMenuItem(systemMenu, SC_CLOSE, d->detectedEscapeButton ?
1742 MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
1745 QDialog::showEvent(e);
1750 QMessageBox::Icon icon,
1751 const QString& title,
const QString& text,
1752 QMessageBox::StandardButtons buttons,
1753 QMessageBox::StandardButton defaultButton)
1757 if (defaultButton && !(buttons & defaultButton)) {
1758 const int defaultButtons = defaultButton | QMessageBox::Default;
1759 const int otherButtons = buttons.toInt();
1760 const int ret = QMessageBoxPrivate::showOldMessageBox(parent, icon, title,
1763 return static_cast<QMessageBox::StandardButton>(ret);
1766 QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
1767 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox*>();
1768 Q_ASSERT(buttonBox !=
nullptr);
1770 uint mask = QMessageBox::FirstButton;
1771 while (mask <= QMessageBox::LastButton) {
1772 uint sb = buttons & mask;
1776 QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
1778 if (msgBox.defaultButton())
1780 if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
1781 || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
1782 msgBox.setDefaultButton(button);
1784 if (msgBox.exec() == -1)
1785 return QMessageBox::Cancel;
1786 return msgBox.standardButton(msgBox.clickedButton());
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812QMessageBox::StandardButton QMessageBox::information(QWidget *parent,
const QString &title,
1813 const QString& text, StandardButtons buttons,
1814 StandardButton defaultButton)
1816 return showNewMessageBox(parent, Information, title, text, buttons,
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844QMessageBox::StandardButton QMessageBox::question(QWidget *parent,
const QString &title,
1845 const QString& text, StandardButtons buttons,
1846 StandardButton defaultButton)
1848 return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874QMessageBox::StandardButton QMessageBox::warning(QWidget *parent,
const QString &title,
1875 const QString& text, StandardButtons buttons,
1876 StandardButton defaultButton)
1878 return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904QMessageBox::StandardButton QMessageBox::critical(QWidget *parent,
const QString &title,
1905 const QString& text, StandardButtons buttons,
1906 StandardButton defaultButton)
1908 return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932void QMessageBox::about(QWidget *parent,
const QString &title,
const QString &text)
1935 static QPointer<QMessageBox> oldMsgBox;
1937 if (oldMsgBox && oldMsgBox->text() == text) {
1940 oldMsgBox->activateWindow();
1945 QMessageBox *msgBox =
new QMessageBox(Information, title, text, NoButton, parent
1947 , Qt::WindowTitleHint | Qt::WindowSystemMenuHint
1950 msgBox->setAttribute(Qt::WA_DeleteOnClose);
1951 QIcon icon = msgBox->windowIcon();
1952 msgBox->setIconPixmap(icon.pixmap(QSize(64, 64), msgBox->devicePixelRatio()));
1957 auto *d = msgBox->d_func();
1958 d->buttonBox->setCenterButtons(
true);
1960 msgBox->setModal(
true);
1962 msgBox->setModal(
false);
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985void QMessageBox::aboutQt(QWidget *parent,
const QString &title)
1988 static QPointer<QMessageBox> oldMsgBox;
1993 oldMsgBox->activateWindow();
1998 QString translatedTextAboutQtCaption;
1999 translatedTextAboutQtCaption = QMessageBox::tr(
2001 "<p>This program uses Qt version %1.</p>"
2002 ).arg(QT_VERSION_STR
""_L1);
2005 const QString translatedTextAboutQtText = QMessageBox::tr(
2006 "<p>Qt is a C++ toolkit for cross-platform application "
2008 "<p>Qt provides single-source portability across all major desktop "
2009 "operating systems. It is also available for embedded Linux and other "
2010 "embedded and mobile operating systems.</p>"
2011 "<p>Qt is available under multiple licensing options designed "
2012 "to accommodate the needs of our various users.</p>"
2013 "<p>Qt licensed under our commercial license agreement is appropriate "
2014 "for development of proprietary/commercial software where you do not "
2015 "want to share any source code with third parties or otherwise cannot "
2016 "comply with the terms of GNU (L)GPL.</p>"
2017 "<p>Qt licensed under GNU (L)GPL is appropriate for the "
2018 "development of Qt applications provided you can comply with the terms "
2019 "and conditions of the respective licenses.</p>"
2020 "<p>Please see <a href=\"https://%2/\">%2</a> "
2021 "for an overview of Qt licensing.</p>"
2022 "<p>Copyright (C) The Qt Company Ltd. and other "
2024 "<p>Qt and the Qt logo are trademarks of The Qt Company Ltd.</p>"
2025 "<p>Qt is The Qt Company Ltd. product developed as an open source "
2026 "project. See <a href=\"https://%3/\">%3</a> for more information.</p>"
2027 ).arg(QStringLiteral(
"qt.io/licensing"),
2028 QStringLiteral(
"qt.io"));
2029 QMessageBox *msgBox =
new QMessageBox(parent);
2030 msgBox->setAttribute(Qt::WA_DeleteOnClose);
2031 msgBox->setWindowTitle(title.isEmpty() ? tr(
"About Qt") : title);
2032 msgBox->setText(translatedTextAboutQtCaption);
2033 msgBox->setInformativeText(translatedTextAboutQtText);
2035 QPixmap pm(
":/qt-project.org/qmessagebox/images/qtlogo-64.png"_L1);
2037 msgBox->setIconPixmap(pm);
2042 auto *d = msgBox->d_func();
2043 d->buttonBox->setCenterButtons(
true);
2045 msgBox->setModal(
true);
2047 msgBox->setModal(
false);
2061 if (button == QMessageBox::NoButton || (button & NewButtonMask))
2062 return QMessageBox::StandardButton(button & QMessageBox::ButtonMask);
2064 return QMessageBox::NoButton;
2083 if (button0 & flags) {
2085 }
else if (button1 & flags) {
2087 }
else if (button2 & flags) {
2090 return q->button(newButton(button));
2096 q->addButton(newButton(button0));
2097 q->addButton(newButton(button1));
2098 q->addButton(newButton(button2));
2099 q->setDefaultButton(
2100 static_cast<QPushButton *>(findButton(button0, button1, button2, QMessageBox::Default)));
2101 q->setEscapeButton(findButton(button0, button1, button2, QMessageBox::Escape));
2107 Q_Q(
const QMessageBox);
2108 QAbstractButton *result = customButtonList.value(id);
2111 if (id & QMessageBox::FlagMask)
2113 return q->button(newButton(id));
2117 const QString &title,
const QString &text,
2118 int button0,
int button1,
int button2)
2120 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2121 messageBox.d_func()->addOldButtons(button0, button1, button2);
2122 return messageBox.exec();
2126 const QString &title,
const QString &text,
2127 const QString &button0Text,
2128 const QString &button1Text,
2129 const QString &button2Text,
2130 int defaultButtonNumber,
2131 int escapeButtonNumber)
2133 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2134 QString myButton0Text = button0Text;
2135 if (myButton0Text.isEmpty())
2136 myButton0Text = QDialogButtonBox::tr(
"OK");
2137 messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
2138 if (!button1Text.isEmpty())
2139 messageBox.addButton(button1Text, QMessageBox::ActionRole);
2140 if (!button2Text.isEmpty())
2141 messageBox.addButton(button2Text, QMessageBox::ActionRole);
2143 const QList<QAbstractButton *> &buttonList = messageBox.d_func()->customButtonList;
2144 messageBox.setDefaultButton(
static_cast<QPushButton *>(buttonList.value(defaultButtonNumber)));
2145 messageBox.setEscapeButton(buttonList.value(escapeButtonNumber));
2151 return buttonList.indexOf(messageBox.clickedButton());
2156#if QT_CONFIG(textedit)
2157 if (detailsButton && detailsText)
2158 detailsButton->setLabel(detailsText->isHidden() ? ShowLabel : HideLabel);
2162#if QT_DEPRECATED_SINCE(6
,2
)
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215QMessageBox::QMessageBox(
const QString &title,
const QString &text, Icon icon,
2216 int button0,
int button1,
int button2, QWidget *parent,
2218 : QDialog(*
new QMessageBoxPrivate, parent,
2219 f | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
2222 d->init(title, text);
2224 d->addOldButtons(button0, button1, button2);
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2267 int button0,
int button1,
int button2)
2269 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2270 button0, button1, button2);
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2303int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2304 const QString& button0Text,
const QString& button1Text,
2305 const QString& button2Text,
int defaultButtonNumber,
2306 int escapeButtonNumber)
2308 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2309 button0Text, button1Text, button2Text,
2310 defaultButtonNumber, escapeButtonNumber);
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2354 int button0,
int button1,
int button2)
2356 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2357 button0, button1, button2);
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2390 const QString& button0Text,
const QString& button1Text,
2391 const QString& button2Text,
int defaultButtonNumber,
2392 int escapeButtonNumber)
2394 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2395 button0Text, button1Text, button2Text,
2396 defaultButtonNumber, escapeButtonNumber);
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2440 int button0,
int button1,
int button2)
2442 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2443 button0, button1, button2);
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2476 const QString& button0Text,
const QString& button1Text,
2477 const QString& button2Text,
int defaultButtonNumber,
2478 int escapeButtonNumber)
2480 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2481 button0Text, button1Text, button2Text,
2482 defaultButtonNumber, escapeButtonNumber);
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2525int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2526 int button0,
int button1,
int button2)
2528 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2529 button0, button1, button2);
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2562 const QString& button0Text,
const QString& button1Text,
2563 const QString& button2Text,
int defaultButtonNumber,
2564 int escapeButtonNumber)
2566 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2567 button0Text, button1Text, button2Text,
2568 defaultButtonNumber, escapeButtonNumber);
2573
2574
2575
2576
2577
2578
2579
2580QString QMessageBox::buttonText(
int button)
const
2582 Q_D(
const QMessageBox);
2584 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2585 return abstractButton->text();
2586 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2588 return QDialogButtonBox::tr(
"OK");
2594
2595
2596
2597
2598
2599
2600
2601
2602void QMessageBox::setButtonText(
int button,
const QString &text)
2605 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2606 abstractButton->setText(text);
2607 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2609 addButton(QMessageBox::Ok)->setText(text);
2615#if QT_CONFIG(textedit)
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626QString QMessageBox::detailedText()
const
2628 Q_D(
const QMessageBox);
2629 return d->detailsText ? d->detailsText->text() : QString();
2632void QMessageBox::setDetailedText(
const QString &text)
2635 if (text.isEmpty()) {
2636 if (d->detailsText) {
2637 d->detailsText->hide();
2638 d->detailsText->deleteLater();
2640 d->detailsText =
nullptr;
2641 removeButton(d->detailsButton);
2642 if (d->detailsButton) {
2643 d->detailsButton->hide();
2644 d->detailsButton->deleteLater();
2646 d->detailsButton =
nullptr;
2648 if (!d->detailsText) {
2649 d->detailsText =
new QMessageBoxDetailsText(
this);
2650 d->detailsText->hide();
2652 if (!d->detailsButton) {
2653 const bool autoAddOkButton = d->autoAddOkButton;
2654 d->detailsButton =
new DetailButton(
this);
2655 addButton(d->detailsButton, QMessageBox::ActionRole);
2656 d->autoAddOkButton = autoAddOkButton;
2658 d->detailsText->setText(text);
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683QString QMessageBox::informativeText()
const
2685 Q_D(
const QMessageBox);
2686 return d->informativeLabel ? d->informativeLabel->text() : QString();
2689void QMessageBox::setInformativeText(
const QString &text)
2692 if (text.isEmpty()) {
2693 if (d->informativeLabel) {
2694 d->informativeLabel->hide();
2695 d->informativeLabel->deleteLater();
2697 d->informativeLabel =
nullptr;
2699 if (!d->informativeLabel) {
2700 QLabel *label =
new QLabel;
2701 label->setObjectName(
"qt_msgbox_informativelabel"_L1);
2702 label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this)));
2703 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
2704 label->setOpenExternalLinks(
true);
2707 label->setFont(qt_app_fonts_hash()->value(
"QTipLabel"));
2709 label->setWordWrap(
true);
2710 label->setTextFormat(d->label->textFormat());
2711 d->informativeLabel = label;
2713 d->informativeLabel->setText(text);
2719
2720
2721
2722
2723
2724
2725void QMessageBox::setWindowTitle(
const QString &title)
2729 QDialog::setWindowTitle(title);
2737
2738
2739
2740
2741
2742
2743
2744
2745void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
2747 QDialog::setWindowModality(windowModality);
2749 if (parentWidget() && windowModality == Qt::WindowModal)
2750 setParent(parentWidget(), Qt::Sheet);
2752 setParent(parentWidget(), Qt::Dialog);
2753 setDefaultButton(d_func()->defaultButton);
2759 QStyle *style = mb ? mb->style() : QApplication::style();
2760 int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize,
nullptr, mb);
2763 case QMessageBox::Information:
2764 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation,
nullptr, mb);
2766 case QMessageBox::Warning:
2767 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr, mb);
2769 case QMessageBox::Critical:
2770 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical,
nullptr, mb);
2772 case QMessageBox::Question:
2773 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion,
nullptr, mb);
2778 if (!tmpIcon.isNull()) {
2779 qreal dpr = mb ? mb->devicePixelRatio() : qApp->devicePixelRatio();
2780 return tmpIcon.pixmap(QSize(iconSize, iconSize), dpr);
2787 auto *messageDialogHelper =
static_cast<QPlatformMessageDialogHelper *>(h);
2788 QObjectPrivate::connect(messageDialogHelper, &QPlatformMessageDialogHelper::clicked,
2789 this, &QMessageBoxPrivate::helperClicked);
2792 QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged,
2793 q_ptr, [
this](Qt::CheckState state) {
2795 checkbox->setCheckState(state);
2798 messageDialogHelper->setOptions(options);
2804 case QMessageBox::NoIcon:
2805 return QMessageDialogOptions::NoIcon;
2806 case QMessageBox::Information:
2807 return QMessageDialogOptions::Information;
2808 case QMessageBox::Warning:
2809 return QMessageDialogOptions::Warning;
2810 case QMessageBox::Critical:
2811 return QMessageDialogOptions::Critical;
2812 case QMessageBox::Question:
2813 return QMessageDialogOptions::Question;
2815 return QMessageDialogOptions::NoIcon;
2820 QPlatformDialogHelper::StandardButtons buttons(
int(q->standardButtons()));
2828 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
2829 if (nativeDialogInUse)
2831 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
2832 || q->testAttribute(Qt::WA_DontShowOnScreen)
2833 || q->testAttribute(Qt::WA_StyleSheet)
2834 || (options->options() & QMessageDialogOptions::Option::DontUseNativeDialog)) {
2839 for (
auto *customButton : buttonBox->buttons()) {
2840 if (QPushButton *pushButton = qobject_cast<QPushButton *>(customButton)) {
2842 if (pushButton->menu())
2848 return QDialogPrivate::canBeNativeDialog();
2854 options->setWindowTitle(q->windowTitle());
2855 options->setText(q->text());
2856 options->setInformativeText(q->informativeText());
2857#if QT_CONFIG(textedit)
2858 options->setDetailedText(q->detailedText());
2860 options->setStandardIcon(helperIcon(q->icon()));
2861 options->setIconPixmap(q->iconPixmap());
2864 options->clearCustomButtons();
2867 auto standardButtons = helperStandardButtons(q);
2868 for (
int button = QDialogButtonBox::StandardButton::FirstButton;
2869 button <= QDialogButtonBox::StandardButton::LastButton; button <<= 1) {
2870 auto *standardButton = buttonBox->button(QDialogButtonBox::StandardButton(button));
2871 if (!standardButton)
2874 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
2875 if (standardButton->text() != platformTheme->standardButtonText(button)) {
2878 const auto buttonRole = buttonBox->buttonRole(standardButton);
2879 options->addButton(standardButton->text(),
2880 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2881 standardButton, button);
2882 standardButtons &= ~QPlatformDialogHelper::StandardButton(button);
2886 if (standardButton == defaultButton)
2887 options->setDefaultButton(button);
2888 else if (standardButton == detectedEscapeButton)
2889 options->setEscapeButton(button);
2891 options->setStandardButtons(standardButtons);
2894 for (
auto *customButton : customButtonList) {
2897 if (customButton == detailsButton)
2900 const auto buttonRole = buttonBox->buttonRole(customButton);
2901 const int buttonId = options->addButton(customButton->text(),
2902 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2905 if (customButton == defaultButton)
2906 options->setDefaultButton(buttonId);
2907 else if (customButton == detectedEscapeButton)
2908 options->setEscapeButton(buttonId);
2912 options->setCheckBox(checkbox->text(), checkbox->checkState());
2917 const auto required = QVersionNumber::fromString(req).normalized();
2918 const auto current = QVersionNumber::fromString(qVersion()).normalized();
2919 if (current >= required)
2921 std::optional<QApplication> application;
2923 application.emplace(argc, argv);
2924 const QString message = QApplication::tr(
"Application \"%1\" requires Qt %2, found Qt %3.")
2925 .arg(qAppName(), required.toString(), current.toString());
2926 QMessageBox::critical(
nullptr, QApplication::tr(
"Incompatible Qt Library Error"),
2927 message, QMessageBox::Abort);
2928 qFatal(
"%ls", qUtf16Printable(message));
2931#if QT_DEPRECATED_SINCE(6
,2
)
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2945QPixmap QMessageBox::standardIcon(Icon icon)
2947 return QMessageBoxPrivate::standardIcon(icon,
nullptr);
2952
2953
2954
2955
2956
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3018#include "moc_qmessagebox.cpp"
3019#include "qmessagebox.moc"
DetailButton * detailsButton
QAbstractButton * abstractButtonForId(int id) const
void initHelper(QPlatformDialogHelper *) override
QPushButton * defaultButton
QLabel * informativeLabel
int execReturnCode(QAbstractButton *button)
QList< QAbstractButton * > customButtonList
QAbstractButton * clickedButton
bool canBeNativeDialog() const override
void addOldButtons(int button0, int button1, int button2)
QAbstractButton * detectedEscapeButton
void buttonClicked(QAbstractButton *)
void retranslateStrings()
QDialogButtonBox * buttonBox
static QPixmap standardIcon(QMessageBox::Icon icon, QMessageBox *mb)
QPointer< QObject > receiverToDisconnectOnClose
QAbstractButton * escapeButton
int dialogCode() const override
void setVisible(bool visible) override
static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, const QString &button0Text, const QString &button1Text, const QString &button2Text, int defaultButtonNumber, int escapeButtonNumber)
static int showOldMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, int button0, int button1, int button2)
QByteArray memberToDisconnectOnClose
void helperPrepareShow(QPlatformDialogHelper *) override
QAbstractButton * findButton(int button0, int button1, int button2, int flags)
void setClickedButton(QAbstractButton *button)
QSharedPointer< QMessageDialogOptions > options
void detectEscapeButton()
void helperClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role)
bool optionsExplicitlySet
QByteArray signalToDisconnectOnClose
static int oldButton(int button)
static QMessageDialogOptions::StandardIcon helperIcon(QMessageBox::Icon i)
static QMessageBox::StandardButton showNewMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
static QMessageBox::StandardButton newButton(int button)
static bool detectedCompat(int button0, int button1, int button2)
void qRequireVersion(int argc, char *argv[], QAnyStringView req)
static QPlatformDialogHelper::StandardButtons helperStandardButtons(QMessageBox *q)