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 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
160 {
return label == ShowLabel ? QMessageBox::tr(
"Show Details...") : QMessageBox::tr(
"Hide Details..."); }
163 { setText(label(lbl)); }
168 QStyleOptionButton opt;
169 initStyleOption(&opt);
170 const QFontMetrics fm = fontMetrics();
171 opt.text = label(ShowLabel);
172 QSize sz = fm.size(Qt::TextShowMnemonic, opt.text);
173 QSize ret = style()->sizeFromContents(QStyle::CT_PushButton, &opt, sz,
this);
174 opt.text = label(HideLabel);
175 sz = fm.size(Qt::TextShowMnemonic, opt.text);
176 ret = ret.expandedTo(style()->sizeFromContents(QStyle::CT_PushButton, &opt, sz,
this));
183 Q_DECLARE_PUBLIC(QMessageBox)
187#if QT_CONFIG(textedit)
197 void helperClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role);
215 const QString &title,
const QString &text,
216 int button0,
int button1,
int button2);
218 const QString &title,
const QString &text,
219 const QString &button0Text,
220 const QString &button1Text,
221 const QString &button2Text,
222 int defaultButtonNumber,
223 int escapeButtonNumber);
242#if QT_CONFIG(textedit)
264 label->setObjectName(
"qt_msgbox_label"_L1);
265 label->setTextInteractionFlags(Qt::TextInteractionFlags(q->style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr, q)));
266 label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
267 label->setOpenExternalLinks(
true);
268 iconLabel =
new QLabel(q);
269 iconLabel->setObjectName(
"qt_msgboxex_icon_label"_L1);
270 iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
272 buttonBox =
new QDialogButtonBox;
273 buttonBox->setObjectName(
"qt_msgbox_buttonbox"_L1);
274 buttonBox->setCenterButtons(q->style()->styleHint(QStyle::SH_MessageBox_CenterButtons,
nullptr, q));
275 QObjectPrivate::connect(buttonBox, &QDialogButtonBox::clicked,
276 this, &QMessageBoxPrivate::buttonClicked);
278 if (!title.isEmpty() || !text.isEmpty()) {
279 q->setWindowTitle(title);
288 icon = QMessageBox::NoIcon;
295 QGridLayout *grid =
new QGridLayout;
296 const bool hasIcon = !iconLabel->pixmap().isNull();
299 grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop);
300 iconLabel->setVisible(hasIcon);
302 QSpacerItem *indentSpacer =
new QSpacerItem(14, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
304 QSpacerItem *indentSpacer =
new QSpacerItem(hasIcon ? 7 : 15, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
306 grid->addItem(indentSpacer, 0, hasIcon ? 1 : 0, 2, 1);
307 grid->addWidget(label, 0, hasIcon ? 2 : 1, 1, 1);
308 if (informativeLabel) {
310 informativeLabel->setContentsMargins(0, 7, 0, 7);
312 grid->addWidget(informativeLabel, 1, hasIcon ? 2 : 1, 1, 1);
315 grid->addWidget(checkbox, informativeLabel ? 2 : 1, hasIcon ? 2 : 1, 1, 1, Qt::AlignLeft);
317 grid->addItem(
new QSpacerItem(1, 15, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0);
319 grid->addItem(
new QSpacerItem(1, 7, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0);
323 grid->addWidget(buttonBox, grid->rowCount(), hasIcon ? 2 : 1, 1, 1);
324 grid->setContentsMargins(0, 0, 0, 0);
325 grid->setVerticalSpacing(8);
326 grid->setHorizontalSpacing(0);
327 q->setContentsMargins(24, 15, 24, 20);
328 grid->setRowStretch(1, 100);
329 grid->setRowMinimumHeight(2, 6);
331 grid->addWidget(buttonBox, grid->rowCount(), 0, 1, grid->columnCount());
333#if QT_CONFIG(textedit)
335 grid->addWidget(detailsText, grid->rowCount(), 0, 1, grid->columnCount());
337 grid->setSizeConstraint(QLayout::SetNoConstraint);
347 return layout->totalMinimumSize().width();
357 const QSize screenSize = q->screen()->availableGeometry().size();
358 int hardLimit = qMin(screenSize.width() - 480, 1000);
360 if (screenSize.width() <= 1024)
361 hardLimit = screenSize.width();
363 int softLimit = qMin(screenSize.width()/2, 420);
366 int softLimit = qMin(screenSize.width()/2, 500);
369 if (informativeLabel)
370 informativeLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
372 label->setWordWrap(
false);
375 if (width > softLimit) {
376 label->setWordWrap(
true);
379 if (width > hardLimit) {
380 label->d_func()->ensureTextControl();
381 if (QWidgetTextControl *control = label->d_func()->control) {
382 QTextOption opt = control->document()->defaultTextOption();
383 opt.setWrapMode(QTextOption::WrapAnywhere);
384 control->document()->setDefaultTextOption(opt);
390 if (informativeLabel) {
391 label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
392 QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Preferred);
393 policy.setHeightForWidth(
true);
394 informativeLabel->setSizePolicy(policy);
396 if (width > hardLimit) {
397 informativeLabel->d_func()->ensureTextControl();
398 if (QWidgetTextControl *control = informativeLabel->d_func()->control) {
399 QTextOption opt = control->document()->defaultTextOption();
400 opt.setWrapMode(QTextOption::WrapAnywhere);
401 control->document()->setDefaultTextOption(opt);
405 policy.setHeightForWidth(label->wordWrap());
406 label->setSizePolicy(policy);
409 QFontMetrics fm(QApplication::font(
"QMdiSubWindowTitleBar"));
410 int windowTitleWidth = qMin(fm.horizontalAdvance(q->windowTitle()) + 50, hardLimit);
411 if (windowTitleWidth > width)
412 width = windowTitleWidth;
415 int height = (layout->hasHeightForWidth())
416 ? layout->totalHeightForWidth(width)
417 : layout->totalMinimumSize().height();
419 q->setFixedSize(width, height);
420 QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);
425 switch (button & QMessageBox::ButtonMask) {
426 case QMessageBox::Ok:
428 case QMessageBox::Cancel:
430 case QMessageBox::Yes:
432 case QMessageBox::No:
434 case QMessageBox::Abort:
436 case QMessageBox::Retry:
438 case QMessageBox::Ignore:
440 case QMessageBox::YesToAll:
442 case QMessageBox::NoToAll:
451 if (
int standardButton = buttonBox->standardButton(button)) {
458 return standardButton;
464 auto customButtonIndex = customButtonList.indexOf(button);
465 if (customButtonIndex >= 0)
466 return QDialog::DialogCode::Accepted + customButtonIndex + 1;
468 return customButtonIndex;
474 Q_Q(
const QMessageBox);
476 if (rescode <= QDialog::Accepted) {
478 }
else if (clickedButton) {
479 switch (q->buttonRole(clickedButton)) {
480 case QMessageBox::AcceptRole:
481 case QMessageBox::YesRole:
482 return QDialog::Accepted;
483 case QMessageBox::RejectRole:
484 case QMessageBox::NoRole:
485 return QDialog::Rejected;
491 return QDialogPrivate::dialogCode();
497#if QT_CONFIG(textedit)
498 if (detailsButton && detailsText && button == detailsButton) {
499 detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel);
500 detailsText->setHidden(!detailsText->isHidden());
505 setClickedButton(button);
507 if (receiverToDisconnectOnClose) {
508 QObject::disconnect(q, signalToDisconnectOnClose, receiverToDisconnectOnClose,
509 memberToDisconnectOnClose);
510 receiverToDisconnectOnClose =
nullptr;
512 signalToDisconnectOnClose.clear();
513 memberToDisconnectOnClose.clear();
521 clickedButton = button;
522 emit q->buttonClicked(clickedButton);
524 auto resultCode = execReturnCode(button);
535 QAbstractButton *dialogButton = helperButton > QPlatformDialogHelper::LastButton ?
536 static_cast<QAbstractButton *>(options->customButton(helperButton)->button) :
537 q->button(QMessageBox::StandardButton(helperButton));
539 Q_ASSERT(dialogButton);
544 dialogButton->click();
548
549
550
551
552
553
554
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
764
765
766
767
770
771
772
773
774
775
776
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
815
816
817
818
821
822
823
824
825
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844QMessageBox::QMessageBox(QWidget *parent)
845 : QDialog(*
new QMessageBoxPrivate, parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869QMessageBox::QMessageBox(Icon icon,
const QString &title,
const QString &text,
870 StandardButtons buttons, QWidget *parent,
872: QDialog(*
new QMessageBoxPrivate, parent, f | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
875 d->init(title, text);
877 if (buttons != NoButton)
878 setStandardButtons(buttons);
882
883
884QMessageBox::~QMessageBox()
889
890
891
892
893
894void QMessageBox::addButton(QAbstractButton *button, ButtonRole role)
899 removeButton(button);
901 if (button->text().isEmpty()) {
902 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
903 if (
auto standardButton = QMessageBoxPrivate::standardButtonForRole(role))
904 button->setText(platformTheme->standardButtonText(standardButton));
907 if (button->text().isEmpty()) {
908 qWarning() <<
"Cannot add" << button <<
"without title";
913 d->buttonBox->addButton(button, (QDialogButtonBox::ButtonRole)role);
914 d->customButtonList.append(button);
915 d->autoAddOkButton =
false;
921 case QMessageBox::AcceptRole:
return QMessageBox::Ok;
922 case QMessageBox::RejectRole:
return QMessageBox::Cancel;
923 case QMessageBox::DestructiveRole:
return QMessageBox::Discard;
924 case QMessageBox::HelpRole:
return QMessageBox::Help;
925 case QMessageBox::ApplyRole:
return QMessageBox::Apply;
926 case QMessageBox::YesRole:
return QMessageBox::Yes;
927 case QMessageBox::NoRole:
return QMessageBox::No;
928 case QMessageBox::ResetRole:
return QMessageBox::Reset;
929 default:
return QMessageBox::NoButton;
934
935
936
937
938
939QPushButton *QMessageBox::addButton(
const QString& text, ButtonRole role)
942 QPushButton *pushButton =
new QPushButton(text);
943 addButton(pushButton, role);
949
950
951
952
953
954
955
956QPushButton *QMessageBox::addButton(StandardButton button)
959 QPushButton *pushButton = d->buttonBox->addButton((QDialogButtonBox::StandardButton)button);
961 d->autoAddOkButton =
false;
966
967
968
969
970void QMessageBox::removeButton(QAbstractButton *button)
973 d->customButtonList.removeAll(button);
974 if (d->escapeButton == button)
975 d->escapeButton =
nullptr;
976 if (d->defaultButton == button)
977 d->defaultButton =
nullptr;
978 d->buttonBox->removeButton(button);
983
984
985
986
987
988
989
990
991
992void QMessageBox::setStandardButtons(StandardButtons buttons)
995 d->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(
int(buttons)));
997 QList<QAbstractButton *> buttonList = d->buttonBox->buttons();
998 if (!buttonList.contains(d->escapeButton))
999 d->escapeButton =
nullptr;
1000 if (!buttonList.contains(d->defaultButton))
1001 d->defaultButton =
nullptr;
1002 d->autoAddOkButton =
false;
1006QMessageBox::StandardButtons QMessageBox::standardButtons()
const
1008 Q_D(
const QMessageBox);
1009 return QMessageBox::StandardButtons(
int(d->buttonBox->standardButtons()));
1013
1014
1015
1016
1017
1018QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button)
const
1020 Q_D(
const QMessageBox);
1021 return (QMessageBox::StandardButton)d->buttonBox->standardButton(button);
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036QAbstractButton *QMessageBox::button(StandardButton which)
const
1038 Q_D(
const QMessageBox);
1039 return d->buttonBox->button(QDialogButtonBox::StandardButton(which));
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060QAbstractButton *QMessageBox::escapeButton()
const
1062 Q_D(
const QMessageBox);
1063 return d->escapeButton;
1067
1068
1069
1070
1071
1072void QMessageBox::setEscapeButton(QAbstractButton *button)
1075 if (d->buttonBox->buttons().contains(button))
1076 d->escapeButton = button;
1080
1081
1082
1083
1084
1085void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)
1088 setEscapeButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
1094 detectedEscapeButton = escapeButton;
1099 detectedEscapeButton = buttonBox->button(QDialogButtonBox::Cancel);
1100 if (detectedEscapeButton)
1104 const QList<QAbstractButton *> buttons = buttonBox->buttons();
1105 if (buttons.size() == 1) {
1106 detectedEscapeButton = buttons.first();
1115 detectedEscapeButton = buttons.at(1 - idx);
1121 for (
auto *button : buttons) {
1122 if (buttonBox->buttonRole(button) == QDialogButtonBox::RejectRole) {
1123 if (detectedEscapeButton) {
1124 detectedEscapeButton =
nullptr;
1127 detectedEscapeButton = button;
1130 if (detectedEscapeButton)
1134 for (
auto *button : buttons) {
1135 if (buttonBox->buttonRole(button) == QDialogButtonBox::NoRole) {
1136 if (detectedEscapeButton) {
1137 detectedEscapeButton =
nullptr;
1140 detectedEscapeButton = button;
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158QAbstractButton *QMessageBox::clickedButton()
const
1160 Q_D(
const QMessageBox);
1161 return d->clickedButton;
1165
1166
1167
1168
1169
1170
1171QPushButton *QMessageBox::defaultButton()
const
1173 Q_D(
const QMessageBox);
1174 return d->defaultButton;
1178
1179
1180
1181
1182
1183void QMessageBox::setDefaultButton(QPushButton *button)
1186 if (!d->buttonBox->buttons().contains(button))
1188 d->defaultButton = button;
1189 button->setDefault(
true);
1194
1195
1196
1197
1198
1199void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)
1202 setDefaultButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
1206
1207
1208
1209
1210
1211
1213void QMessageBox::setCheckBox(QCheckBox *cb)
1217 if (cb == d->checkbox)
1221 d->checkbox->hide();
1222 layout()->removeWidget(d->checkbox);
1223 if (d->checkbox->parentWidget() ==
this) {
1224 d->checkbox->setParent(
nullptr);
1225 d->checkbox->deleteLater();
1230 QSizePolicy sp = d->checkbox->sizePolicy();
1231 sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
1232 d->checkbox->setSizePolicy(sp);
1239
1240
1241
1242
1244QCheckBox* QMessageBox::checkBox()
const
1246 Q_D(
const QMessageBox);
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266void QMessageBox::setOption(QMessageBox::Option option,
bool on)
1268 const QMessageBox::Options previousOptions = options();
1269 if (!(previousOptions & option) != !on)
1270 setOptions(previousOptions ^ option);
1274
1275
1276
1277
1278
1279
1280
1281bool QMessageBox::testOption(QMessageBox::Option option)
const
1283 Q_D(
const QMessageBox);
1284 return d->options->testOption(
static_cast<QMessageDialogOptions::Option>(option));
1287void QMessageBox::setOptions(QMessageBox::Options options)
1291 if (QMessageBox::options() == options)
1294 d->options->setOptions(QMessageDialogOptions::Option(
int(options)));
1297QMessageBox::Options QMessageBox::options()
const
1299 Q_D(
const QMessageBox);
1300 return QMessageBox::Options(
int(d->options->options()));
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338QString QMessageBox::text()
const
1340 Q_D(
const QMessageBox);
1341 return d->label->text();
1344void QMessageBox::setText(
const QString &text)
1347 d->label->setText(text);
1348 d->label->setWordWrap(d->label->textFormat() == Qt::RichText
1349 || (d->label->textFormat() == Qt::AutoText && Qt::mightBeRichText(text)));
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398QMessageBox::Icon QMessageBox::icon()
const
1400 Q_D(
const QMessageBox);
1404void QMessageBox::setIcon(Icon icon)
1407 setIconPixmap(QMessageBoxPrivate::standardIcon((QMessageBox::Icon)icon,
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424QPixmap QMessageBox::iconPixmap()
const
1426 Q_D(
const QMessageBox);
1427 return d->iconLabel->pixmap();
1430void QMessageBox::setIconPixmap(
const QPixmap &pixmap)
1433 d->iconLabel->setPixmap(pixmap);
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449Qt::TextFormat QMessageBox::textFormat()
const
1451 Q_D(
const QMessageBox);
1452 return d->label->textFormat();
1455void QMessageBox::setTextFormat(Qt::TextFormat format)
1458 d->label->setTextFormat(format);
1459 d->label->setWordWrap(format == Qt::RichText
1460 || (format == Qt::AutoText && Qt::mightBeRichText(d->label->text())));
1461 if (d->informativeLabel)
1462 d->informativeLabel->setTextFormat(format);
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1478Qt::TextInteractionFlags QMessageBox::textInteractionFlags()
const
1480 Q_D(
const QMessageBox);
1481 return d->label->textInteractionFlags();
1484void QMessageBox::setTextInteractionFlags(Qt::TextInteractionFlags flags)
1487 d->label->setTextInteractionFlags(flags);
1491
1492
1493bool QMessageBox::event(QEvent *e)
1495 bool result =QDialog::event(e);
1496 switch (e->type()) {
1497 case QEvent::LayoutRequest:
1498 d_func()->updateSize();
1500 case QEvent::LanguageChange:
1501 d_func()->retranslateStrings();
1510
1511
1512void QMessageBox::resizeEvent(QResizeEvent *event)
1514 QDialog::resizeEvent(event);
1518
1519
1520void QMessageBox::closeEvent(QCloseEvent *e)
1523 if (!d->detectedEscapeButton) {
1527 QDialog::closeEvent(e);
1528 if (!d->clickedButton) {
1529 d->clickedButton = d->detectedEscapeButton;
1530 setResult(d->execReturnCode(d->detectedEscapeButton));
1535
1536
1537void QMessageBox::changeEvent(QEvent *ev)
1540 switch (ev->type()) {
1541 case QEvent::StyleChange:
1543 if (d->icon != NoIcon)
1545 Qt::TextInteractionFlags flags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this));
1546 d->label->setTextInteractionFlags(flags);
1547 d->buttonBox->setCenterButtons(style()->styleHint(QStyle::SH_MessageBox_CenterButtons,
nullptr,
this));
1548 if (d->informativeLabel)
1549 d->informativeLabel->setTextInteractionFlags(flags);
1552 case QEvent::FontChange:
1553 case QEvent::ApplicationFontChange:
1558 d->label->setFont(f);
1565 QDialog::changeEvent(ev);
1569
1570
1571void QMessageBox::keyPressEvent(QKeyEvent *e)
1573#if QT_CONFIG(shortcut)
1575 if (e->matches(QKeySequence::Cancel)) {
1576 if (d->detectedEscapeButton) {
1578 d->detectedEscapeButton->animateClick();
1580 d->detectedEscapeButton->click();
1587#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
1589#if QT_CONFIG(textedit)
1590 if (e == QKeySequence::Copy) {
1591 if (d->detailsText && d->detailsText->isVisible() && d->detailsText->copy()) {
1592 e->setAccepted(
true);
1595 }
else if (e == QKeySequence::SelectAll && d->detailsText && d->detailsText->isVisible()) {
1596 d->detailsText->selectAll();
1597 e->setAccepted(
true);
1602#if defined(Q_OS_WIN)
1603 if (e == QKeySequence::Copy) {
1604 const auto separator =
"---------------------------\n"_L1;
1606 textToCopy += separator + windowTitle() + u'\n' + separator
1607 + d->label->text() + u'\n' + separator;
1609 if (d->informativeLabel)
1610 textToCopy += d->informativeLabel->text() + u'\n' + separator;
1612 const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
1613 for (
const auto *button : buttons)
1614 textToCopy += button->text() +
" "_L1;
1615 textToCopy += u'\n' + separator;
1616#if QT_CONFIG(textedit)
1618 textToCopy += d->detailsText->text() + u'\n' + separator;
1620 QGuiApplication::clipboard()->setText(textToCopy);
1627#ifndef QT_NO_SHORTCUT
1628 if (!(e->modifiers() & (Qt::AltModifier | Qt::ControlModifier | Qt::MetaModifier))) {
1629 int key = e->key() & ~Qt::MODIFIER_MASK;
1631 const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
1632 for (
auto *pb : buttons) {
1633 QKeySequence shortcut = pb->shortcut();
1634 if (!shortcut.isEmpty() && key == shortcut[0].key()) {
1642 QDialog::keyPressEvent(e);
1646
1647
1648
1649
1650
1651
1652
1653void QMessageBox::open(QObject *receiver,
const char *member)
1656 const char *signal = member && strchr(member,
'*') ? SIGNAL(buttonClicked(QAbstractButton*))
1657 : SIGNAL(finished(
int));
1658 connect(
this, signal, receiver, member);
1659 d->signalToDisconnectOnClose = signal;
1660 d->receiverToDisconnectOnClose = receiver;
1661 d->memberToDisconnectOnClose = member;
1670 if (autoAddOkButton)
1671 q->addButton(QMessageBox::Ok);
1678 setNativeDialogVisible(visible);
1683 static_cast<QWidget*>(q_ptr)->setAttribute(Qt::WA_DontShowOnScreen, nativeDialogInUse);
1685 QDialogPrivate::setVisible(visible);
1689
1690
1691
1692
1693QList<QAbstractButton *> QMessageBox::buttons()
const
1695 Q_D(
const QMessageBox);
1696 return d->buttonBox->buttons();
1700
1701
1702
1703
1704
1705QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button)
const
1707 Q_D(
const QMessageBox);
1708 return QMessageBox::ButtonRole(d->buttonBox->buttonRole(button));
1712
1713
1714void QMessageBox::showEvent(QShowEvent *e)
1717 d->clickedButton =
nullptr;
1720#if QT_CONFIG(accessibility)
1721 QAccessibleEvent event(
this, QAccessible::Alert);
1722 QAccessible::updateAccessibility(&event);
1724#if defined(Q_OS_WIN)
1725 if (
const HMENU systemMenu = qt_getWindowsSystemMenu(
this)) {
1726 EnableMenuItem(systemMenu, SC_CLOSE, d->detectedEscapeButton ?
1727 MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
1730 QDialog::showEvent(e);
1735 QMessageBox::Icon icon,
1736 const QString& title,
const QString& text,
1737 QMessageBox::StandardButtons buttons,
1738 QMessageBox::StandardButton defaultButton)
1742 if (defaultButton && !(buttons & defaultButton)) {
1743 const int defaultButtons = defaultButton | QMessageBox::Default;
1744 const int otherButtons = buttons.toInt();
1745 const int ret = QMessageBoxPrivate::showOldMessageBox(parent, icon, title,
1748 return static_cast<QMessageBox::StandardButton>(ret);
1751 QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
1752 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox*>();
1753 Q_ASSERT(buttonBox !=
nullptr);
1755 uint mask = QMessageBox::FirstButton;
1756 while (mask <= QMessageBox::LastButton) {
1757 uint sb = buttons & mask;
1761 QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
1763 if (msgBox.defaultButton())
1765 if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
1766 || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
1767 msgBox.setDefaultButton(button);
1769 if (msgBox.exec() == -1)
1770 return QMessageBox::Cancel;
1771 return msgBox.standardButton(msgBox.clickedButton());
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797QMessageBox::StandardButton QMessageBox::information(QWidget *parent,
const QString &title,
1798 const QString& text, StandardButtons buttons,
1799 StandardButton defaultButton)
1801 return showNewMessageBox(parent, Information, title, text, buttons,
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829QMessageBox::StandardButton QMessageBox::question(QWidget *parent,
const QString &title,
1830 const QString& text, StandardButtons buttons,
1831 StandardButton defaultButton)
1833 return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859QMessageBox::StandardButton QMessageBox::warning(QWidget *parent,
const QString &title,
1860 const QString& text, StandardButtons buttons,
1861 StandardButton defaultButton)
1863 return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889QMessageBox::StandardButton QMessageBox::critical(QWidget *parent,
const QString &title,
1890 const QString& text, StandardButtons buttons,
1891 StandardButton defaultButton)
1893 return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917void QMessageBox::about(QWidget *parent,
const QString &title,
const QString &text)
1920 static QPointer<QMessageBox> oldMsgBox;
1922 if (oldMsgBox && oldMsgBox->text() == text) {
1925 oldMsgBox->activateWindow();
1930 QMessageBox *msgBox =
new QMessageBox(Information, title, text, NoButton, parent
1932 , Qt::WindowTitleHint | Qt::WindowSystemMenuHint
1935 msgBox->setAttribute(Qt::WA_DeleteOnClose);
1936 QIcon icon = msgBox->windowIcon();
1937 msgBox->setIconPixmap(icon.pixmap(QSize(64, 64), msgBox->devicePixelRatio()));
1942 auto *d = msgBox->d_func();
1943 d->buttonBox->setCenterButtons(
true);
1945 msgBox->setModal(
true);
1947 msgBox->setModal(
false);
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970void QMessageBox::aboutQt(QWidget *parent,
const QString &title)
1973 static QPointer<QMessageBox> oldMsgBox;
1978 oldMsgBox->activateWindow();
1983 QString translatedTextAboutQtCaption;
1984 translatedTextAboutQtCaption = QMessageBox::tr(
1986 "<p>This program uses Qt version %1.</p>"
1987 ).arg(QT_VERSION_STR
""_L1);
1990 const QString translatedTextAboutQtText = QMessageBox::tr(
1991 "<p>Qt is a C++ toolkit for cross-platform application "
1993 "<p>Qt provides single-source portability across all major desktop "
1994 "operating systems. It is also available for embedded Linux and other "
1995 "embedded and mobile operating systems.</p>"
1996 "<p>Qt is available under multiple licensing options designed "
1997 "to accommodate the needs of our various users.</p>"
1998 "<p>Qt licensed under our commercial license agreement is appropriate "
1999 "for development of proprietary/commercial software where you do not "
2000 "want to share any source code with third parties or otherwise cannot "
2001 "comply with the terms of GNU (L)GPL.</p>"
2002 "<p>Qt licensed under GNU (L)GPL is appropriate for the "
2003 "development of Qt applications provided you can comply with the terms "
2004 "and conditions of the respective licenses.</p>"
2005 "<p>Please see <a href=\"https://%2/\">%2</a> "
2006 "for an overview of Qt licensing.</p>"
2007 "<p>Copyright (C) The Qt Company Ltd. and other "
2009 "<p>Qt and the Qt logo are trademarks of The Qt Company Ltd.</p>"
2010 "<p>Qt is The Qt Company Ltd. product developed as an open source "
2011 "project. See <a href=\"https://%3/\">%3</a> for more information.</p>"
2012 ).arg(QStringLiteral(
"qt.io/licensing"),
2013 QStringLiteral(
"qt.io"));
2014 QMessageBox *msgBox =
new QMessageBox(parent);
2015 msgBox->setAttribute(Qt::WA_DeleteOnClose);
2016 msgBox->setWindowTitle(title.isEmpty() ? tr(
"About Qt") : title);
2017 msgBox->setText(translatedTextAboutQtCaption);
2018 msgBox->setInformativeText(translatedTextAboutQtText);
2020 QPixmap pm(
":/qt-project.org/qmessagebox/images/qtlogo-64.png"_L1);
2022 msgBox->setIconPixmap(pm);
2027 auto *d = msgBox->d_func();
2028 d->buttonBox->setCenterButtons(
true);
2030 msgBox->setModal(
true);
2032 msgBox->setModal(
false);
2046 if (button == QMessageBox::NoButton || (button & NewButtonMask))
2047 return QMessageBox::StandardButton(button & QMessageBox::ButtonMask);
2049 return QMessageBox::NoButton;
2068 if (button0 & flags) {
2070 }
else if (button1 & flags) {
2072 }
else if (button2 & flags) {
2075 return q->button(newButton(button));
2081 q->addButton(newButton(button0));
2082 q->addButton(newButton(button1));
2083 q->addButton(newButton(button2));
2084 q->setDefaultButton(
2085 static_cast<QPushButton *>(findButton(button0, button1, button2, QMessageBox::Default)));
2086 q->setEscapeButton(findButton(button0, button1, button2, QMessageBox::Escape));
2092 Q_Q(
const QMessageBox);
2093 QAbstractButton *result = customButtonList.value(id);
2096 if (id & QMessageBox::FlagMask)
2098 return q->button(newButton(id));
2102 const QString &title,
const QString &text,
2103 int button0,
int button1,
int button2)
2105 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2106 messageBox.d_func()->addOldButtons(button0, button1, button2);
2107 return messageBox.exec();
2111 const QString &title,
const QString &text,
2112 const QString &button0Text,
2113 const QString &button1Text,
2114 const QString &button2Text,
2115 int defaultButtonNumber,
2116 int escapeButtonNumber)
2118 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2119 QString myButton0Text = button0Text;
2120 if (myButton0Text.isEmpty())
2121 myButton0Text = QDialogButtonBox::tr(
"OK");
2122 messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
2123 if (!button1Text.isEmpty())
2124 messageBox.addButton(button1Text, QMessageBox::ActionRole);
2125 if (!button2Text.isEmpty())
2126 messageBox.addButton(button2Text, QMessageBox::ActionRole);
2128 const QList<QAbstractButton *> &buttonList = messageBox.d_func()->customButtonList;
2129 messageBox.setDefaultButton(
static_cast<QPushButton *>(buttonList.value(defaultButtonNumber)));
2130 messageBox.setEscapeButton(buttonList.value(escapeButtonNumber));
2136 return buttonList.indexOf(messageBox.clickedButton());
2141#if QT_CONFIG(textedit)
2142 if (detailsButton && detailsText)
2143 detailsButton->setLabel(detailsText->isHidden() ? ShowLabel : HideLabel);
2147#if QT_DEPRECATED_SINCE(6
,2
)
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
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
2200QMessageBox::QMessageBox(
const QString &title,
const QString &text, Icon icon,
2201 int button0,
int button1,
int button2, QWidget *parent,
2203 : QDialog(*
new QMessageBoxPrivate, parent,
2204 f | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
2207 d->init(title, text);
2209 d->addOldButtons(button0, button1, button2);
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2252 int button0,
int button1,
int button2)
2254 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2255 button0, button1, button2);
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
2288int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2289 const QString& button0Text,
const QString& button1Text,
2290 const QString& button2Text,
int defaultButtonNumber,
2291 int escapeButtonNumber)
2293 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2294 button0Text, button1Text, button2Text,
2295 defaultButtonNumber, escapeButtonNumber);
2299
2300
2301
2302
2303
2304
2305
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
2338int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2339 int button0,
int button1,
int button2)
2341 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2342 button0, button1, button2);
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2375 const QString& button0Text,
const QString& button1Text,
2376 const QString& button2Text,
int defaultButtonNumber,
2377 int escapeButtonNumber)
2379 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2380 button0Text, button1Text, button2Text,
2381 defaultButtonNumber, escapeButtonNumber);
2386
2387
2388
2389
2390
2391
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
2424int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2425 int button0,
int button1,
int button2)
2427 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2428 button0, button1, button2);
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2461 const QString& button0Text,
const QString& button1Text,
2462 const QString& button2Text,
int defaultButtonNumber,
2463 int escapeButtonNumber)
2465 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2466 button0Text, button1Text, button2Text,
2467 defaultButtonNumber, escapeButtonNumber);
2471
2472
2473
2474
2475
2476
2477
2478
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
2510int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2511 int button0,
int button1,
int button2)
2513 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2514 button0, button1, button2);
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2547 const QString& button0Text,
const QString& button1Text,
2548 const QString& button2Text,
int defaultButtonNumber,
2549 int escapeButtonNumber)
2551 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2552 button0Text, button1Text, button2Text,
2553 defaultButtonNumber, escapeButtonNumber);
2558
2559
2560
2561
2562
2563
2564
2565QString QMessageBox::buttonText(
int button)
const
2567 Q_D(
const QMessageBox);
2569 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2570 return abstractButton->text();
2571 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2573 return QDialogButtonBox::tr(
"OK");
2579
2580
2581
2582
2583
2584
2585
2586
2587void QMessageBox::setButtonText(
int button,
const QString &text)
2590 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2591 abstractButton->setText(text);
2592 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2594 addButton(QMessageBox::Ok)->setText(text);
2600#if QT_CONFIG(textedit)
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611QString QMessageBox::detailedText()
const
2613 Q_D(
const QMessageBox);
2614 return d->detailsText ? d->detailsText->text() : QString();
2617void QMessageBox::setDetailedText(
const QString &text)
2620 if (text.isEmpty()) {
2621 if (d->detailsText) {
2622 d->detailsText->hide();
2623 d->detailsText->deleteLater();
2625 d->detailsText =
nullptr;
2626 removeButton(d->detailsButton);
2627 if (d->detailsButton) {
2628 d->detailsButton->hide();
2629 d->detailsButton->deleteLater();
2631 d->detailsButton =
nullptr;
2633 if (!d->detailsText) {
2634 d->detailsText =
new QMessageBoxDetailsText(
this);
2635 d->detailsText->hide();
2637 if (!d->detailsButton) {
2638 const bool autoAddOkButton = d->autoAddOkButton;
2639 d->detailsButton =
new DetailButton(
this);
2640 addButton(d->detailsButton, QMessageBox::ActionRole);
2641 d->autoAddOkButton = autoAddOkButton;
2643 d->detailsText->setText(text);
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668QString QMessageBox::informativeText()
const
2670 Q_D(
const QMessageBox);
2671 return d->informativeLabel ? d->informativeLabel->text() : QString();
2674void QMessageBox::setInformativeText(
const QString &text)
2677 if (text.isEmpty()) {
2678 if (d->informativeLabel) {
2679 d->informativeLabel->hide();
2680 d->informativeLabel->deleteLater();
2682 d->informativeLabel =
nullptr;
2684 if (!d->informativeLabel) {
2685 QLabel *label =
new QLabel;
2686 label->setObjectName(
"qt_msgbox_informativelabel"_L1);
2687 label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this)));
2688 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
2689 label->setOpenExternalLinks(
true);
2692 label->setFont(qt_app_fonts_hash()->value(
"QTipLabel"));
2694 label->setWordWrap(
true);
2695 label->setTextFormat(d->label->textFormat());
2696 d->informativeLabel = label;
2698 d->informativeLabel->setText(text);
2704
2705
2706
2707
2708
2709
2710void QMessageBox::setWindowTitle(
const QString &title)
2714 QDialog::setWindowTitle(title);
2722
2723
2724
2725
2726
2727
2728
2729
2730void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
2732 QDialog::setWindowModality(windowModality);
2734 if (parentWidget() && windowModality == Qt::WindowModal)
2735 setParent(parentWidget(), Qt::Sheet);
2737 setParent(parentWidget(), Qt::Dialog);
2738 setDefaultButton(d_func()->defaultButton);
2744 QStyle *style = mb ? mb->style() : QApplication::style();
2745 int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize,
nullptr, mb);
2748 case QMessageBox::Information:
2749 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation,
nullptr, mb);
2751 case QMessageBox::Warning:
2752 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr, mb);
2754 case QMessageBox::Critical:
2755 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical,
nullptr, mb);
2757 case QMessageBox::Question:
2758 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion,
nullptr, mb);
2763 if (!tmpIcon.isNull()) {
2764 qreal dpr = mb ? mb->devicePixelRatio() : qApp->devicePixelRatio();
2765 return tmpIcon.pixmap(QSize(iconSize, iconSize), dpr);
2772 auto *messageDialogHelper =
static_cast<QPlatformMessageDialogHelper *>(h);
2773 QObjectPrivate::connect(messageDialogHelper, &QPlatformMessageDialogHelper::clicked,
2774 this, &QMessageBoxPrivate::helperClicked);
2777 QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged,
2778 q_ptr, [
this](Qt::CheckState state) {
2780 checkbox->setCheckState(state);
2783 messageDialogHelper->setOptions(options);
2789 case QMessageBox::NoIcon:
2790 return QMessageDialogOptions::NoIcon;
2791 case QMessageBox::Information:
2792 return QMessageDialogOptions::Information;
2793 case QMessageBox::Warning:
2794 return QMessageDialogOptions::Warning;
2795 case QMessageBox::Critical:
2796 return QMessageDialogOptions::Critical;
2797 case QMessageBox::Question:
2798 return QMessageDialogOptions::Question;
2800 return QMessageDialogOptions::NoIcon;
2805 QPlatformDialogHelper::StandardButtons buttons(
int(q->standardButtons()));
2813 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
2814 if (nativeDialogInUse)
2816 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
2817 || q->testAttribute(Qt::WA_DontShowOnScreen)
2818 || q->testAttribute(Qt::WA_StyleSheet)
2819 || (options->options() & QMessageDialogOptions::Option::DontUseNativeDialog)) {
2823 if (strcmp(QMessageBox::staticMetaObject.className(), q->metaObject()->className()) != 0)
2827 for (
auto *customButton : buttonBox->buttons()) {
2828 if (QPushButton *pushButton = qobject_cast<QPushButton *>(customButton)) {
2830 if (pushButton->menu())
2836 return QDialogPrivate::canBeNativeDialog();
2842 options->setWindowTitle(q->windowTitle());
2843 options->setText(q->text());
2844 options->setInformativeText(q->informativeText());
2845#if QT_CONFIG(textedit)
2846 options->setDetailedText(q->detailedText());
2848 options->setStandardIcon(helperIcon(q->icon()));
2849 options->setIconPixmap(q->iconPixmap());
2852 options->clearCustomButtons();
2855 auto standardButtons = helperStandardButtons(q);
2856 for (
int button = QDialogButtonBox::StandardButton::FirstButton;
2857 button <= QDialogButtonBox::StandardButton::LastButton; button <<= 1) {
2858 auto *standardButton = buttonBox->button(QDialogButtonBox::StandardButton(button));
2859 if (!standardButton)
2862 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
2863 if (standardButton->text() != platformTheme->standardButtonText(button)) {
2866 const auto buttonRole = buttonBox->buttonRole(standardButton);
2867 options->addButton(standardButton->text(),
2868 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2869 standardButton, button);
2870 standardButtons &= ~QPlatformDialogHelper::StandardButton(button);
2874 if (standardButton == defaultButton)
2875 options->setDefaultButton(button);
2876 else if (standardButton == detectedEscapeButton)
2877 options->setEscapeButton(button);
2879 options->setStandardButtons(standardButtons);
2882 for (
auto *customButton : customButtonList) {
2885 if (customButton == detailsButton)
2888 const auto buttonRole = buttonBox->buttonRole(customButton);
2889 const int buttonId = options->addButton(customButton->text(),
2890 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2893 if (customButton == defaultButton)
2894 options->setDefaultButton(buttonId);
2895 else if (customButton == detectedEscapeButton)
2896 options->setEscapeButton(buttonId);
2900 options->setCheckBox(checkbox->text(), checkbox->checkState());
2905 const auto required = QVersionNumber::fromString(req).normalized();
2906 const auto current = QVersionNumber::fromString(qVersion()).normalized();
2907 if (current >= required)
2909 std::optional<QApplication> application;
2911 application.emplace(argc, argv);
2912 const QString message = QApplication::tr(
"Application \"%1\" requires Qt %2, found Qt %3.")
2913 .arg(qAppName(), required.toString(), current.toString());
2914 QMessageBox::critical(
nullptr, QApplication::tr(
"Incompatible Qt Library Error"),
2915 message, QMessageBox::Abort);
2916 qFatal(
"%ls", qUtf16Printable(message));
2919#if QT_DEPRECATED_SINCE(6
,2
)
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2933QPixmap QMessageBox::standardIcon(Icon icon)
2935 return QMessageBoxPrivate::standardIcon(icon,
nullptr);
2940
2941
2942
2943
2944
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3006#include "moc_qmessagebox.cpp"
3007#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)
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)