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/qplatformwindow_p.h>
48using namespace Qt::StringLiterals;
51HMENU qt_getWindowsSystemMenu(
const QWidget *w)
53 if (QWindow *window = QApplicationPrivate::windowForWidget(w))
54 if (
auto *windowInterface = window->nativeInterface<QNativeInterface::Private::QWindowsWindow>())
55 return GetSystemMenu(windowInterface->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,
1752 const QString& text,
1753 QMessageBox::StandardButtons buttons,
1754 QMessageBox::StandardButton defaultButton,
1755 std::optional<Qt::TextFormat> textFormat =
std::nullopt)
1759 if (defaultButton && !(buttons & defaultButton)) {
1760 const int defaultButtons = defaultButton | QMessageBox::Default;
1761 const int otherButtons = buttons.toInt();
1762 const int ret = QMessageBoxPrivate::showOldMessageBox(parent, icon, title,
1765 return static_cast<QMessageBox::StandardButton>(ret);
1768 QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
1770 msgBox.setTextFormat(textFormat.value());
1771 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox*>();
1772 Q_ASSERT(buttonBox !=
nullptr);
1774 uint mask = QMessageBox::FirstButton;
1775 while (mask <= QMessageBox::LastButton) {
1776 uint sb = buttons & mask;
1780 QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
1782 if (msgBox.defaultButton())
1784 if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
1785 || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
1786 msgBox.setDefaultButton(button);
1788 if (msgBox.exec() == -1)
1789 return QMessageBox::Cancel;
1790 return msgBox.standardButton(msgBox.clickedButton());
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804QMessageBox::StandardButton QMessageBox::information(QWidget *parent,
1805 const QString &title,
1806 const QString& text,
1807 Qt::TextFormat textFormat,
1808 StandardButtons buttons,
1809 StandardButton defaultButton)
1811 return showNewMessageBox(parent,
1821
1822
1823
1824
1825
1826
1827
1828
1829QMessageBox::StandardButton QMessageBox::information(QWidget *parent,
const QString &title,
1830 const QString& text, StandardButtons buttons,
1831 StandardButton defaultButton)
1833 return showNewMessageBox(parent, Information, title, text, buttons,
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848QMessageBox::StandardButton QMessageBox::question(QWidget *parent,
1849 const QString &title,
1850 const QString &text,
1851 Qt::TextFormat textFormat,
1852 StandardButtons buttons,
1853 StandardButton defaultButton)
1855 return showNewMessageBox(parent,
1865
1866
1867
1868
1869
1870
1871
1872
1873QMessageBox::StandardButton QMessageBox::question(QWidget *parent,
const QString &title,
1874 const QString& text, StandardButtons buttons,
1875 StandardButton defaultButton)
1877 return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891QMessageBox::StandardButton QMessageBox::warning(QWidget *parent,
1892 const QString &title,
1893 const QString &text,
1894 Qt::TextFormat textFormat,
1895 StandardButtons buttons,
1896 StandardButton defaultButton)
1898 return showNewMessageBox(parent,
1908
1909
1910
1911
1912
1913
1914
1915
1916QMessageBox::StandardButton QMessageBox::warning(QWidget *parent,
const QString &title,
1917 const QString& text, StandardButtons buttons,
1918 StandardButton defaultButton)
1920 return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934QMessageBox::StandardButton QMessageBox::critical(QWidget *parent,
1935 const QString &title,
1936 const QString &text,
1937 Qt::TextFormat textFormat,
1938 StandardButtons buttons,
1939 StandardButton defaultButton)
1941 return showNewMessageBox(parent,
1951
1952
1953
1954
1955
1956
1957
1958
1959QMessageBox::StandardButton QMessageBox::critical(QWidget *parent,
const QString &title,
1960 const QString& text, StandardButtons buttons,
1961 StandardButton defaultButton)
1963 return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
1967 const QString &title,
1968 const QString &text,
1969 std::optional<Qt::TextFormat> textFormat =
std::nullopt)
1972 static QPointer<QMessageBox> oldMsgBox;
1974 if (oldMsgBox && oldMsgBox->text() == text
1975 && (!textFormat || oldMsgBox->textFormat() == textFormat.value())) {
1978 oldMsgBox->activateWindow();
1983 QMessageBox *msgBox =
new QMessageBox(QMessageBox::Information, title, text,
1984 QMessageBox::NoButton, parent
1986 , Qt::WindowTitleHint | Qt::WindowSystemMenuHint
1989 msgBox->setAttribute(Qt::WA_DeleteOnClose);
1990 QIcon icon = msgBox->windowIcon();
1991 msgBox->setIconPixmap(icon.pixmap(QSize(64, 64), msgBox->devicePixelRatio()));
1993 msgBox->setTextFormat(textFormat.value());
1998 QDialogButtonBox *buttonBox = msgBox->findChild<QDialogButtonBox *>();
1999 Q_ASSERT(buttonBox !=
nullptr);
2000 buttonBox->setCenterButtons(
true);
2002 msgBox->setModal(
true);
2004 msgBox->setModal(
false);
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023void QMessageBox::about(QWidget *parent,
2024 const QString &title,
2025 const QString &text,
2026 Qt::TextFormat textFormat)
2028 showAboutBox(parent, title, text, textFormat);
2032
2033
2034
2035
2036
2037
2038
2039
2040void QMessageBox::about(QWidget *parent,
const QString &title,
const QString &text)
2042 showAboutBox(parent, title, text);
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060void QMessageBox::aboutQt(QWidget *parent,
const QString &title)
2063 static QPointer<QMessageBox> oldMsgBox;
2068 oldMsgBox->activateWindow();
2073 QString translatedTextAboutQtCaption;
2074 translatedTextAboutQtCaption = QMessageBox::tr(
2076 "<p>This program uses Qt version %1.</p>"
2077 ).arg(QT_VERSION_STR
""_L1);
2080 const QString translatedTextAboutQtText = QMessageBox::tr(
2081 "<p>Qt is a C++ toolkit for cross-platform application "
2083 "<p>Qt provides single-source portability across all major desktop "
2084 "operating systems. It is also available for embedded Linux and other "
2085 "embedded and mobile operating systems.</p>"
2086 "<p>Qt is available under multiple licensing options designed "
2087 "to accommodate the needs of our various users.</p>"
2088 "<p>Qt licensed under our commercial license agreement is appropriate "
2089 "for development of proprietary/commercial software where you do not "
2090 "want to share any source code with third parties or otherwise cannot "
2091 "comply with the terms of GNU (L)GPL.</p>"
2092 "<p>Qt licensed under GNU (L)GPL is appropriate for the "
2093 "development of Qt applications provided you can comply with the terms "
2094 "and conditions of the respective licenses.</p>"
2095 "<p>Please see <a href=\"https://%2/\">%2</a> "
2096 "for an overview of Qt licensing.</p>"
2097 "<p>Copyright (C) The Qt Company Ltd. and other "
2099 "<p>Qt and the Qt logo are trademarks of The Qt Company Ltd.</p>"
2100 "<p>Qt is The Qt Company Ltd. product developed as an open source "
2101 "project. See <a href=\"https://%3/\">%3</a> for more information.</p>"
2102 ).arg(QStringLiteral(
"qt.io/licensing"),
2103 QStringLiteral(
"qt.io"));
2104 QMessageBox *msgBox =
new QMessageBox(parent);
2105 msgBox->setTextFormat(Qt::RichText);
2106 msgBox->setAttribute(Qt::WA_DeleteOnClose);
2107 msgBox->setWindowTitle(title.isEmpty() ? tr(
"About Qt") : title);
2108 msgBox->setText(translatedTextAboutQtCaption);
2109 msgBox->setInformativeText(translatedTextAboutQtText);
2111 QPixmap pm(
":/qt-project.org/qmessagebox/images/qtlogo-64.png"_L1);
2113 msgBox->setIconPixmap(pm);
2118 auto *d = msgBox->d_func();
2119 d->buttonBox->setCenterButtons(
true);
2121 msgBox->setModal(
true);
2123 msgBox->setModal(
false);
2137 if (button == QMessageBox::NoButton || (button & NewButtonMask))
2138 return QMessageBox::StandardButton(button & QMessageBox::ButtonMask);
2140 return QMessageBox::NoButton;
2159 if (button0 & flags) {
2161 }
else if (button1 & flags) {
2163 }
else if (button2 & flags) {
2166 return q->button(newButton(button));
2172 q->addButton(newButton(button0));
2173 q->addButton(newButton(button1));
2174 q->addButton(newButton(button2));
2175 q->setDefaultButton(
2176 static_cast<QPushButton *>(findButton(button0, button1, button2, QMessageBox::Default)));
2177 q->setEscapeButton(findButton(button0, button1, button2, QMessageBox::Escape));
2183 Q_Q(
const QMessageBox);
2184 QAbstractButton *result = customButtonList.value(id);
2187 if (id & QMessageBox::FlagMask)
2189 return q->button(newButton(id));
2193 const QString &title,
const QString &text,
2194 int button0,
int button1,
int button2)
2196 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2197 messageBox.setTextFormat(Qt::AutoText);
2198 messageBox.d_func()->addOldButtons(button0, button1, button2);
2199 return messageBox.exec();
2203 const QString &title,
const QString &text,
2204 const QString &button0Text,
2205 const QString &button1Text,
2206 const QString &button2Text,
2207 int defaultButtonNumber,
2208 int escapeButtonNumber)
2210 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2211 messageBox.setTextFormat(Qt::AutoText);
2212 QString myButton0Text = button0Text;
2213 if (myButton0Text.isEmpty())
2214 myButton0Text = QDialogButtonBox::tr(
"OK");
2215 messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
2216 if (!button1Text.isEmpty())
2217 messageBox.addButton(button1Text, QMessageBox::ActionRole);
2218 if (!button2Text.isEmpty())
2219 messageBox.addButton(button2Text, QMessageBox::ActionRole);
2221 const QList<QAbstractButton *> &buttonList = messageBox.d_func()->customButtonList;
2222 messageBox.setDefaultButton(
static_cast<QPushButton *>(buttonList.value(defaultButtonNumber)));
2223 messageBox.setEscapeButton(buttonList.value(escapeButtonNumber));
2229 return buttonList.indexOf(messageBox.clickedButton());
2234#if QT_CONFIG(textedit)
2235 if (detailsButton && detailsText)
2236 detailsButton->setLabel(detailsText->isHidden() ? ShowLabel : HideLabel);
2240#if QT_DEPRECATED_SINCE(6
,2
)
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293QMessageBox::QMessageBox(
const QString &title,
const QString &text, Icon icon,
2294 int button0,
int button1,
int button2, QWidget *parent,
2296 : QDialog(*
new QMessageBoxPrivate, parent,
2297 f | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
2300 d->init(title, text);
2302 d->addOldButtons(button0, button1, button2);
2306
2307
2308
2309
2310
2311
2312
2313
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
2344int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2345 int button0,
int button1,
int button2)
2347 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2348 button0, button1, button2);
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2381int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2382 const QString& button0Text,
const QString& button1Text,
2383 const QString& button2Text,
int defaultButtonNumber,
2384 int escapeButtonNumber)
2386 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2387 button0Text, button1Text, button2Text,
2388 defaultButtonNumber, escapeButtonNumber);
2392
2393
2394
2395
2396
2397
2398
2399
2400
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
2431int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2432 int button0,
int button1,
int button2)
2434 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2435 button0, button1, button2);
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2468 const QString& button0Text,
const QString& button1Text,
2469 const QString& button2Text,
int defaultButtonNumber,
2470 int escapeButtonNumber)
2472 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2473 button0Text, button1Text, button2Text,
2474 defaultButtonNumber, escapeButtonNumber);
2479
2480
2481
2482
2483
2484
2485
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
2517int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2518 int button0,
int button1,
int button2)
2520 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2521 button0, button1, button2);
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2554 const QString& button0Text,
const QString& button1Text,
2555 const QString& button2Text,
int defaultButtonNumber,
2556 int escapeButtonNumber)
2558 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2559 button0Text, button1Text, button2Text,
2560 defaultButtonNumber, escapeButtonNumber);
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2603int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2604 int button0,
int button1,
int button2)
2606 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2607 button0, button1, button2);
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2640 const QString& button0Text,
const QString& button1Text,
2641 const QString& button2Text,
int defaultButtonNumber,
2642 int escapeButtonNumber)
2644 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2645 button0Text, button1Text, button2Text,
2646 defaultButtonNumber, escapeButtonNumber);
2651
2652
2653
2654
2655
2656
2657
2658QString QMessageBox::buttonText(
int button)
const
2660 Q_D(
const QMessageBox);
2662 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2663 return abstractButton->text();
2664 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2666 return QDialogButtonBox::tr(
"OK");
2672
2673
2674
2675
2676
2677
2678
2679
2680void QMessageBox::setButtonText(
int button,
const QString &text)
2683 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2684 abstractButton->setText(text);
2685 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2687 addButton(QMessageBox::Ok)->setText(text);
2693#if QT_CONFIG(textedit)
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704QString QMessageBox::detailedText()
const
2706 Q_D(
const QMessageBox);
2707 return d->detailsText ? d->detailsText->text() : QString();
2710void QMessageBox::setDetailedText(
const QString &text)
2713 if (text.isEmpty()) {
2714 if (d->detailsText) {
2715 d->detailsText->hide();
2716 d->detailsText->deleteLater();
2718 d->detailsText =
nullptr;
2719 removeButton(d->detailsButton);
2720 if (d->detailsButton) {
2721 d->detailsButton->hide();
2722 d->detailsButton->deleteLater();
2724 d->detailsButton =
nullptr;
2726 if (!d->detailsText) {
2727 d->detailsText =
new QMessageBoxDetailsText(
this);
2728 d->detailsText->hide();
2730 if (!d->detailsButton) {
2731 const bool autoAddOkButton = d->autoAddOkButton;
2732 d->detailsButton =
new DetailButton(
this);
2733 addButton(d->detailsButton, QMessageBox::ActionRole);
2734 d->autoAddOkButton = autoAddOkButton;
2736 d->detailsText->setText(text);
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761QString QMessageBox::informativeText()
const
2763 Q_D(
const QMessageBox);
2764 return d->informativeLabel ? d->informativeLabel->text() : QString();
2767void QMessageBox::setInformativeText(
const QString &text)
2770 if (text.isEmpty()) {
2771 if (d->informativeLabel) {
2772 d->informativeLabel->hide();
2773 d->informativeLabel->deleteLater();
2775 d->informativeLabel =
nullptr;
2777 if (!d->informativeLabel) {
2778 QLabel *label =
new QLabel;
2779 label->setObjectName(
"qt_msgbox_informativelabel"_L1);
2780 label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this)));
2781 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
2782 label->setOpenExternalLinks(
true);
2785 label->setFont(qt_app_fonts_hash()->value(
"QTipLabel"));
2787 label->setWordWrap(
true);
2788 label->setTextFormat(d->label->textFormat());
2789 d->informativeLabel = label;
2791 d->informativeLabel->setText(text);
2797
2798
2799
2800
2801
2802
2803void QMessageBox::setWindowTitle(
const QString &title)
2807 QDialog::setWindowTitle(title);
2815
2816
2817
2818
2819
2820
2821
2822
2823void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
2825 QDialog::setWindowModality(windowModality);
2827 if (parentWidget() && windowModality == Qt::WindowModal)
2828 setParent(parentWidget(), Qt::Sheet);
2830 setParent(parentWidget(), Qt::Dialog);
2831 setDefaultButton(d_func()->defaultButton);
2837 QStyle *style = mb ? mb->style() : QApplication::style();
2838 int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize,
nullptr, mb);
2841 case QMessageBox::Information:
2842 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation,
nullptr, mb);
2844 case QMessageBox::Warning:
2845 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr, mb);
2847 case QMessageBox::Critical:
2848 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical,
nullptr, mb);
2850 case QMessageBox::Question:
2851 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion,
nullptr, mb);
2856 if (!tmpIcon.isNull()) {
2857 qreal dpr = mb ? mb->devicePixelRatio() : qApp->devicePixelRatio();
2858 return tmpIcon.pixmap(QSize(iconSize, iconSize), dpr);
2865 auto *messageDialogHelper =
static_cast<QPlatformMessageDialogHelper *>(h);
2866 QObjectPrivate::connect(messageDialogHelper, &QPlatformMessageDialogHelper::clicked,
2867 this, &QMessageBoxPrivate::helperClicked);
2870 QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged,
2871 q_ptr, [
this](Qt::CheckState state) {
2873 checkbox->setCheckState(state);
2876 messageDialogHelper->setOptions(options);
2882 case QMessageBox::NoIcon:
2883 return QMessageDialogOptions::NoIcon;
2884 case QMessageBox::Information:
2885 return QMessageDialogOptions::Information;
2886 case QMessageBox::Warning:
2887 return QMessageDialogOptions::Warning;
2888 case QMessageBox::Critical:
2889 return QMessageDialogOptions::Critical;
2890 case QMessageBox::Question:
2891 return QMessageDialogOptions::Question;
2893 return QMessageDialogOptions::NoIcon;
2898 QPlatformDialogHelper::StandardButtons buttons(
int(q->standardButtons()));
2906 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
2907 if (nativeDialogInUse)
2909 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
2910 || q->testAttribute(Qt::WA_DontShowOnScreen)
2911 || q->testAttribute(Qt::WA_StyleSheet)
2912 || (options->options() & QMessageDialogOptions::Option::DontUseNativeDialog)) {
2917 for (
auto *customButton : buttonBox->buttons()) {
2918 if (QPushButton *pushButton = qobject_cast<QPushButton *>(customButton)) {
2920 if (pushButton->menu())
2926 return QDialogPrivate::canBeNativeDialog();
2932 options->setWindowTitle(q->windowTitle());
2933 options->setText(q->text());
2934 options->setInformativeText(q->informativeText());
2935#if QT_CONFIG(textedit)
2936 options->setDetailedText(q->detailedText());
2938 options->setStandardIcon(helperIcon(q->icon()));
2939 options->setIconPixmap(q->iconPixmap());
2942 options->clearCustomButtons();
2945 auto standardButtons = helperStandardButtons(q);
2946 for (
int button = QDialogButtonBox::StandardButton::FirstButton;
2947 button <= QDialogButtonBox::StandardButton::LastButton; button <<= 1) {
2948 auto *standardButton = buttonBox->button(QDialogButtonBox::StandardButton(button));
2949 if (!standardButton)
2952 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
2953 if (standardButton->text() != platformTheme->standardButtonText(button)) {
2956 const auto buttonRole = buttonBox->buttonRole(standardButton);
2957 options->addButton(standardButton->text(),
2958 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2959 standardButton, button);
2960 standardButtons &= ~QPlatformDialogHelper::StandardButton(button);
2964 if (standardButton == defaultButton)
2965 options->setDefaultButton(button);
2966 else if (standardButton == detectedEscapeButton)
2967 options->setEscapeButton(button);
2969 options->setStandardButtons(standardButtons);
2972 for (
auto *customButton : customButtonList) {
2975 if (customButton == detailsButton)
2978 const auto buttonRole = buttonBox->buttonRole(customButton);
2979 const int buttonId = options->addButton(customButton->text(),
2980 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2983 if (customButton == defaultButton)
2984 options->setDefaultButton(buttonId);
2985 else if (customButton == detectedEscapeButton)
2986 options->setEscapeButton(buttonId);
2990 options->setCheckBox(checkbox->text(), checkbox->checkState());
2995 const auto required = QVersionNumber::fromString(req).normalized();
2996 const auto current = QVersionNumber::fromString(qVersion()).normalized();
2997 if (current >= required)
2999 std::optional<QApplication> application;
3001 application.emplace(argc, argv);
3002 const QString message = QApplication::tr(
"Application \"%1\" requires Qt %2, found Qt %3.")
3003 .arg(qAppName(), required.toString(), current.toString());
3004 QMessageBox::critical(
nullptr, QApplication::tr(
"Incompatible Qt Library Error"),
3005 message, QMessageBox::Abort);
3006 qFatal(
"%ls", qUtf16Printable(message));
3009#if QT_DEPRECATED_SINCE(6
,2
)
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3023QPixmap QMessageBox::standardIcon(Icon icon)
3025 return QMessageBoxPrivate::standardIcon(icon,
nullptr);
3030
3031
3032
3033
3034
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3096#include "moc_qmessagebox.cpp"
3097#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 QMessageBox::StandardButton showNewMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton, std::optional< Qt::TextFormat > textFormat=std::nullopt)
static QMessageDialogOptions::StandardIcon helperIcon(QMessageBox::Icon i)
static QMessageBox::StandardButton newButton(int button)
static void showAboutBox(QWidget *parent, const QString &title, const QString &text, std::optional< Qt::TextFormat > textFormat=std::nullopt)
static bool detectedCompat(int button0, int button1, int button2)
void qRequireVersion(int argc, char *argv[], QAnyStringView req)
static QPlatformDialogHelper::StandardButtons helperStandardButtons(QMessageBox *q)