5#include <QtWidgets/qmessagebox.h>
7#include <QtWidgets/qdialogbuttonbox.h>
8#include "private/qlabel_p.h"
9#include "private/qapplication_p.h"
10#include <QtCore/qlist.h>
11#include <QtCore/qdebug.h>
12#include <QtWidgets/qstyle.h>
13#include <QtWidgets/qstyleoption.h>
14#include <QtWidgets/qgridlayout.h>
15#include <QtWidgets/qpushbutton.h>
16#include <QtWidgets/qcheckbox.h>
17#include <QtGui/qaccessible.h>
18#include <QtGui/qicon.h>
19#include <QtGui/qtextdocument.h>
20#include <QtWidgets/qapplication.h>
21#if QT_CONFIG(textedit)
22#include <QtWidgets/qtextedit.h>
25#include <QtWidgets/qmenu.h>
28#include <QtGui/qfont.h>
29#include <QtGui/qfontmetrics.h>
30#include <QtGui/qclipboard.h>
31#include "private/qabstractbutton_p.h"
32#include <QtGui/qpa/qplatformtheme.h>
34#include <QtCore/qanystringview.h>
35#include <QtCore/qdebug.h>
36#include <QtCore/qpointer.h>
37#include <QtCore/qversionnumber.h>
40# include <QtCore/qt_windows.h>
41#include <qpa/qplatformnativeinterface.h>
48using namespace Qt::StringLiterals;
51HMENU qt_getWindowsSystemMenu(
const QWidget *w)
53 if (QWindow *window = QApplicationPrivate::windowForWidget(w))
54 if (
void *handle = QGuiApplication::platformNativeInterface()->nativeResourceForWindow(
"handle", window))
55 return GetSystemMenu(
reinterpret_cast<HWND>(handle),
false);
60static_assert(qToUnderlying(QMessageBox::ButtonRole::NRoles) ==
61 qToUnderlying(QDialogButtonBox::ButtonRole::NRoles),
62 "QMessageBox::ButtonRole and QDialogButtonBox::ButtonRole out of sync!");
64static_assert(std::is_same_v<std::underlying_type_t<QMessageBox::ButtonRole>,
65 std::underlying_type_t<QDialogButtonBox::ButtonRole>>);
70static_assert(
static_cast<
int>(QMessageBox::StandardButton::LastButton) ==
71 static_cast<
int>(QDialogButtonBox::StandardButton::LastButton),
72 "QMessageBox::StandardButton and QDialogButtonBox::StandardButton out of sync!");
79#if QT_CONFIG(textedit)
80class QMessageBoxDetailsText :
public QWidget
84 class TextEdit :
public QTextEdit
87 TextEdit(QWidget *parent=
nullptr) : QTextEdit(parent) { }
88#ifndef QT_NO_CONTEXTMENU
89 void contextMenuEvent(QContextMenuEvent * e) override
91 if (QMenu *menu = createStandardContextMenu()) {
92 menu->setAttribute(Qt::WA_DeleteOnClose);
93 menu->popup(e->globalPos());
99 QMessageBoxDetailsText(QWidget *parent=
nullptr)
101 , copyAvailable(
false)
103 QVBoxLayout *layout =
new QVBoxLayout;
104 layout->setContentsMargins(QMargins());
105 QFrame *line =
new QFrame(
this);
106 line->setFrameShape(QFrame::HLine);
107 line->setFrameShadow(QFrame::Sunken);
108 layout->addWidget(line);
109 textEdit =
new TextEdit();
110 textEdit->setFixedHeight(100);
111 textEdit->setFocusPolicy(Qt::NoFocus);
112 textEdit->setReadOnly(
true);
113 layout->addWidget(textEdit);
116 connect(textEdit, &TextEdit::copyAvailable,
117 this, &QMessageBoxDetailsText::textCopyAvailable);
119 void setText(
const QString &text) { textEdit->setPlainText(text); }
120 QString text()
const {
return textEdit->toPlainText(); }
124#ifdef QT_NO_CLIPBOARD
136 textEdit->selectAll();
140 void textCopyAvailable(
bool available)
142 copyAvailable = available;
156#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU_ONLY >= 1600
&& Q_CC_GNU_ONLY < 1700
&& !defined(__OPTIMIZE__)
189 Q_DECLARE_PUBLIC(QMessageBox)
193#if QT_CONFIG(textedit)
203 void helperClicked(QPlatformDialogHelper::StandardButton button, QPlatformDialogHelper::ButtonRole role);
221 const QString &title,
const QString &text,
222 int button0,
int button1,
int button2);
224 const QString &title,
const QString &text,
225 const QString &button0Text,
226 const QString &button1Text,
227 const QString &button2Text,
228 int defaultButtonNumber,
229 int escapeButtonNumber);
248#if QT_CONFIG(textedit)
270 label->setObjectName(
"qt_msgbox_label"_L1);
271 label->setTextInteractionFlags(Qt::TextInteractionFlags(q->style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr, q)));
272 label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
273 label->setOpenExternalLinks(
true);
274 iconLabel =
new QLabel(q);
275 iconLabel->setObjectName(
"qt_msgboxex_icon_label"_L1);
276 iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
278 buttonBox =
new QDialogButtonBox;
279 buttonBox->setObjectName(
"qt_msgbox_buttonbox"_L1);
280 buttonBox->setCenterButtons(q->style()->styleHint(QStyle::SH_MessageBox_CenterButtons,
nullptr, q));
281 QObjectPrivate::connect(buttonBox, &QDialogButtonBox::clicked,
282 this, &QMessageBoxPrivate::buttonClicked);
284 if (!title.isEmpty() || !text.isEmpty()) {
285 q->setWindowTitle(title);
294 icon = QMessageBox::NoIcon;
301 QGridLayout *grid =
new QGridLayout;
302 const bool hasIcon = !iconLabel->pixmap().isNull();
305 grid->addWidget(iconLabel, 0, 0, 2, 1, Qt::AlignTop);
306 iconLabel->setVisible(hasIcon);
308 QSpacerItem *indentSpacer =
new QSpacerItem(14, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
310 QSpacerItem *indentSpacer =
new QSpacerItem(hasIcon ? 7 : 15, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
312 grid->addItem(indentSpacer, 0, hasIcon ? 1 : 0, 2, 1);
313 grid->addWidget(label, 0, hasIcon ? 2 : 1, 1, 1);
314 if (informativeLabel) {
316 informativeLabel->setContentsMargins(0, 7, 0, 7);
318 grid->addWidget(informativeLabel, 1, hasIcon ? 2 : 1, 1, 1);
321 grid->addWidget(checkbox, informativeLabel ? 2 : 1, hasIcon ? 2 : 1, 1, 1, Qt::AlignLeft);
323 grid->addItem(
new QSpacerItem(1, 15, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0);
325 grid->addItem(
new QSpacerItem(1, 7, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0);
329 grid->addWidget(buttonBox, grid->rowCount(), hasIcon ? 2 : 1, 1, 1);
330 grid->setContentsMargins(0, 0, 0, 0);
331 grid->setVerticalSpacing(8);
332 grid->setHorizontalSpacing(0);
333 q->setContentsMargins(24, 15, 24, 20);
334 grid->setRowStretch(1, 100);
335 grid->setRowMinimumHeight(2, 6);
337 grid->addWidget(buttonBox, grid->rowCount(), 0, 1, grid->columnCount());
339#if QT_CONFIG(textedit)
341 grid->addWidget(detailsText, grid->rowCount(), 0, 1, grid->columnCount());
343 grid->setSizeConstraint(QLayout::SetNoConstraint);
353 return layout->totalMinimumSize().width();
363 const QSize screenSize = q->screen()->availableGeometry().size();
364 int hardLimit = qMin(screenSize.width() - 480, 1000);
366 if (screenSize.width() <= 1024)
367 hardLimit = screenSize.width();
369 int softLimit = qMin(screenSize.width()/2, 420);
372 int softLimit = qMin(screenSize.width()/2, 500);
375 if (informativeLabel)
376 informativeLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
378 label->setWordWrap(
false);
381 if (width > softLimit) {
382 label->setWordWrap(
true);
385 if (width > hardLimit) {
386 label->d_func()->ensureTextControl();
387 if (QWidgetTextControl *control = label->d_func()->control) {
388 QTextOption opt = control->document()->defaultTextOption();
389 opt.setWrapMode(QTextOption::WrapAnywhere);
390 control->document()->setDefaultTextOption(opt);
396 if (informativeLabel) {
397 label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
398 QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Preferred);
399 policy.setHeightForWidth(
true);
400 informativeLabel->setSizePolicy(policy);
402 if (width > hardLimit) {
403 informativeLabel->d_func()->ensureTextControl();
404 if (QWidgetTextControl *control = informativeLabel->d_func()->control) {
405 QTextOption opt = control->document()->defaultTextOption();
406 opt.setWrapMode(QTextOption::WrapAnywhere);
407 control->document()->setDefaultTextOption(opt);
411 policy.setHeightForWidth(label->wordWrap());
412 label->setSizePolicy(policy);
415 QFontMetrics fm(QApplication::font(
"QMdiSubWindowTitleBar"));
416 int windowTitleWidth = qMin(fm.horizontalAdvance(q->windowTitle()) + 50, hardLimit);
417 if (windowTitleWidth > width)
418 width = windowTitleWidth;
421 int height = (layout->hasHeightForWidth())
422 ? layout->totalHeightForWidth(width)
423 : layout->totalMinimumSize().height();
425 q->setFixedSize(width, height);
426 QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);
431 switch (button & QMessageBox::ButtonMask) {
432 case QMessageBox::Ok:
434 case QMessageBox::Cancel:
436 case QMessageBox::Yes:
438 case QMessageBox::No:
440 case QMessageBox::Abort:
442 case QMessageBox::Retry:
444 case QMessageBox::Ignore:
446 case QMessageBox::YesToAll:
448 case QMessageBox::NoToAll:
457 if (
int standardButton = buttonBox->standardButton(button)) {
464 return standardButton;
470 auto customButtonIndex = customButtonList.indexOf(button);
471 if (customButtonIndex >= 0)
472 return QDialog::DialogCode::Accepted + customButtonIndex + 1;
474 return customButtonIndex;
480 Q_Q(
const QMessageBox);
482 if (rescode <= QDialog::Accepted) {
484 }
else if (clickedButton) {
485 switch (q->buttonRole(clickedButton)) {
486 case QMessageBox::AcceptRole:
487 case QMessageBox::YesRole:
488 return QDialog::Accepted;
489 case QMessageBox::RejectRole:
490 case QMessageBox::NoRole:
491 return QDialog::Rejected;
497 return QDialogPrivate::dialogCode();
503#if QT_CONFIG(textedit)
504 if (detailsButton && detailsText && button == detailsButton) {
505 detailsButton->setLabel(detailsText->isHidden() ? HideLabel : ShowLabel);
506 detailsText->setHidden(!detailsText->isHidden());
511 setClickedButton(button);
513 if (receiverToDisconnectOnClose) {
514 QObject::disconnect(q, signalToDisconnectOnClose, receiverToDisconnectOnClose,
515 memberToDisconnectOnClose);
516 receiverToDisconnectOnClose =
nullptr;
518 signalToDisconnectOnClose.clear();
519 memberToDisconnectOnClose.clear();
527 clickedButton = button;
528 emit q->buttonClicked(clickedButton);
530 auto resultCode = execReturnCode(button);
541 QAbstractButton *dialogButton = helperButton > QPlatformDialogHelper::LastButton ?
542 static_cast<QAbstractButton *>(options->customButton(helperButton)->button) :
543 q->button(QMessageBox::StandardButton(helperButton));
545 Q_ASSERT(dialogButton);
550 dialogButton->click();
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
762
763
764
765
766
767
770
771
772
773
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
813
814
815
816
817
818
821
822
823
824
827
828
829
830
831
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850QMessageBox::QMessageBox(QWidget *parent)
851 : QDialog(*
new QMessageBoxPrivate, parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875QMessageBox::QMessageBox(Icon icon,
const QString &title,
const QString &text,
876 StandardButtons buttons, QWidget *parent,
878: QDialog(*
new QMessageBoxPrivate, parent, f | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
881 d->init(title, text);
883 if (buttons != NoButton)
884 setStandardButtons(buttons);
888
889
890QMessageBox::~QMessageBox()
895
896
897
898
899
900void QMessageBox::addButton(QAbstractButton *button, ButtonRole role)
905 removeButton(button);
907 if (button->text().isEmpty()) {
908 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
909 if (
auto standardButton = QMessageBoxPrivate::standardButtonForRole(role))
910 button->setText(platformTheme->standardButtonText(standardButton));
913 if (button->text().isEmpty()) {
914 qWarning() <<
"Cannot add" << button <<
"without title";
919 d->buttonBox->addButton(button, (QDialogButtonBox::ButtonRole)role);
920 d->customButtonList.append(button);
921 d->autoAddOkButton =
false;
927 case QMessageBox::AcceptRole:
return QMessageBox::Ok;
928 case QMessageBox::RejectRole:
return QMessageBox::Cancel;
929 case QMessageBox::DestructiveRole:
return QMessageBox::Discard;
930 case QMessageBox::HelpRole:
return QMessageBox::Help;
931 case QMessageBox::ApplyRole:
return QMessageBox::Apply;
932 case QMessageBox::YesRole:
return QMessageBox::Yes;
933 case QMessageBox::NoRole:
return QMessageBox::No;
934 case QMessageBox::ResetRole:
return QMessageBox::Reset;
935 default:
return QMessageBox::NoButton;
940
941
942
943
944
945QPushButton *QMessageBox::addButton(
const QString& text, ButtonRole role)
948 QPushButton *pushButton =
new QPushButton(text);
949 addButton(pushButton, role);
955
956
957
958
959
960
961
962QPushButton *QMessageBox::addButton(StandardButton button)
965 QPushButton *pushButton = d->buttonBox->addButton((QDialogButtonBox::StandardButton)button);
967 d->autoAddOkButton =
false;
972
973
974
975
976void QMessageBox::removeButton(QAbstractButton *button)
979 d->customButtonList.removeAll(button);
980 if (d->escapeButton == button)
981 d->escapeButton =
nullptr;
982 if (d->defaultButton == button)
983 d->defaultButton =
nullptr;
984 d->buttonBox->removeButton(button);
989
990
991
992
993
994
995
996
997
998void QMessageBox::setStandardButtons(StandardButtons buttons)
1001 d->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(
int(buttons)));
1003 QList<QAbstractButton *> buttonList = d->buttonBox->buttons();
1004 if (!buttonList.contains(d->escapeButton))
1005 d->escapeButton =
nullptr;
1006 if (!buttonList.contains(d->defaultButton))
1007 d->defaultButton =
nullptr;
1008 d->autoAddOkButton =
false;
1012QMessageBox::StandardButtons QMessageBox::standardButtons()
const
1014 Q_D(
const QMessageBox);
1015 return QMessageBox::StandardButtons(
int(d->buttonBox->standardButtons()));
1019
1020
1021
1022
1023
1024QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button)
const
1026 Q_D(
const QMessageBox);
1027 return (QMessageBox::StandardButton)d->buttonBox->standardButton(button);
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042QAbstractButton *QMessageBox::button(StandardButton which)
const
1044 Q_D(
const QMessageBox);
1045 return d->buttonBox->button(QDialogButtonBox::StandardButton(which));
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066QAbstractButton *QMessageBox::escapeButton()
const
1068 Q_D(
const QMessageBox);
1069 return d->escapeButton;
1073
1074
1075
1076
1077
1078void QMessageBox::setEscapeButton(QAbstractButton *button)
1081 if (d->buttonBox->buttons().contains(button))
1082 d->escapeButton = button;
1086
1087
1088
1089
1090
1091void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)
1094 setEscapeButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
1100 detectedEscapeButton = escapeButton;
1105 detectedEscapeButton = buttonBox->button(QDialogButtonBox::Cancel);
1106 if (detectedEscapeButton)
1110 const QList<QAbstractButton *> buttons = buttonBox->buttons();
1111 if (buttons.size() == 1) {
1112 detectedEscapeButton = buttons.first();
1121 detectedEscapeButton = buttons.at(1 - idx);
1127 for (
auto *button : buttons) {
1128 if (buttonBox->buttonRole(button) == QDialogButtonBox::RejectRole) {
1129 if (detectedEscapeButton) {
1130 detectedEscapeButton =
nullptr;
1133 detectedEscapeButton = button;
1136 if (detectedEscapeButton)
1140 for (
auto *button : buttons) {
1141 if (buttonBox->buttonRole(button) == QDialogButtonBox::NoRole) {
1142 if (detectedEscapeButton) {
1143 detectedEscapeButton =
nullptr;
1146 detectedEscapeButton = button;
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164QAbstractButton *QMessageBox::clickedButton()
const
1166 Q_D(
const QMessageBox);
1167 return d->clickedButton;
1171
1172
1173
1174
1175
1176
1177QPushButton *QMessageBox::defaultButton()
const
1179 Q_D(
const QMessageBox);
1180 return d->defaultButton;
1184
1185
1186
1187
1188
1189void QMessageBox::setDefaultButton(QPushButton *button)
1192 if (!d->buttonBox->buttons().contains(button))
1194 d->defaultButton = button;
1195 button->setDefault(
true);
1200
1201
1202
1203
1204
1205void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)
1208 setDefaultButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button)));
1212
1213
1214
1215
1216
1217
1219void QMessageBox::setCheckBox(QCheckBox *cb)
1223 if (cb == d->checkbox)
1227 d->checkbox->hide();
1228 layout()->removeWidget(d->checkbox);
1229 if (d->checkbox->parentWidget() ==
this) {
1230 d->checkbox->setParent(
nullptr);
1231 d->checkbox->deleteLater();
1236 QSizePolicy sp = d->checkbox->sizePolicy();
1237 sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding);
1238 d->checkbox->setSizePolicy(sp);
1245
1246
1247
1248
1250QCheckBox* QMessageBox::checkBox()
const
1252 Q_D(
const QMessageBox);
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272void QMessageBox::setOption(QMessageBox::Option option,
bool on)
1274 const QMessageBox::Options previousOptions = options();
1275 if (!(previousOptions & option) != !on)
1276 setOptions(previousOptions ^ option);
1280
1281
1282
1283
1284
1285
1286
1287bool QMessageBox::testOption(QMessageBox::Option option)
const
1289 Q_D(
const QMessageBox);
1290 return d->options->testOption(
static_cast<QMessageDialogOptions::Option>(option));
1293void QMessageBox::setOptions(QMessageBox::Options options)
1297 if (QMessageBox::options() == options)
1300 d->options->setOptions(QMessageDialogOptions::Option(
int(options)));
1303QMessageBox::Options QMessageBox::options()
const
1305 Q_D(
const QMessageBox);
1306 return QMessageBox::Options(
int(d->options->options()));
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344QString QMessageBox::text()
const
1346 Q_D(
const QMessageBox);
1347 return d->label->text();
1350void QMessageBox::setText(
const QString &text)
1353 d->label->setText(text);
1354 d->label->setWordWrap(d->label->textFormat() == Qt::RichText
1355 || (d->label->textFormat() == Qt::AutoText && Qt::mightBeRichText(text)));
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404QMessageBox::Icon QMessageBox::icon()
const
1406 Q_D(
const QMessageBox);
1410void QMessageBox::setIcon(Icon icon)
1413 setIconPixmap(QMessageBoxPrivate::standardIcon((QMessageBox::Icon)icon,
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430QPixmap QMessageBox::iconPixmap()
const
1432 Q_D(
const QMessageBox);
1433 return d->iconLabel->pixmap();
1436void QMessageBox::setIconPixmap(
const QPixmap &pixmap)
1439 d->iconLabel->setPixmap(pixmap);
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455Qt::TextFormat QMessageBox::textFormat()
const
1457 Q_D(
const QMessageBox);
1458 return d->label->textFormat();
1461void QMessageBox::setTextFormat(Qt::TextFormat format)
1464 d->label->setTextFormat(format);
1465 d->label->setWordWrap(format == Qt::RichText
1466 || (format == Qt::AutoText && Qt::mightBeRichText(d->label->text())));
1467 if (d->informativeLabel)
1468 d->informativeLabel->setTextFormat(format);
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1484Qt::TextInteractionFlags QMessageBox::textInteractionFlags()
const
1486 Q_D(
const QMessageBox);
1487 return d->label->textInteractionFlags();
1490void QMessageBox::setTextInteractionFlags(Qt::TextInteractionFlags flags)
1493 d->label->setTextInteractionFlags(flags);
1497
1498
1499bool QMessageBox::event(QEvent *e)
1501 bool result =QDialog::event(e);
1502 switch (e->type()) {
1503 case QEvent::LayoutRequest:
1504 d_func()->updateSize();
1506 case QEvent::LanguageChange:
1507 d_func()->retranslateStrings();
1516
1517
1518void QMessageBox::resizeEvent(QResizeEvent *event)
1520 QDialog::resizeEvent(event);
1524
1525
1526void QMessageBox::closeEvent(QCloseEvent *e)
1529 if (!d->detectedEscapeButton) {
1533 QDialog::closeEvent(e);
1534 if (!d->clickedButton) {
1535 d->clickedButton = d->detectedEscapeButton;
1536 setResult(d->execReturnCode(d->detectedEscapeButton));
1541
1542
1543void QMessageBox::changeEvent(QEvent *ev)
1546 switch (ev->type()) {
1547 case QEvent::StyleChange:
1549 if (d->icon != NoIcon)
1551 Qt::TextInteractionFlags flags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this));
1552 d->label->setTextInteractionFlags(flags);
1553 d->buttonBox->setCenterButtons(style()->styleHint(QStyle::SH_MessageBox_CenterButtons,
nullptr,
this));
1554 if (d->informativeLabel)
1555 d->informativeLabel->setTextInteractionFlags(flags);
1558 case QEvent::FontChange:
1559 case QEvent::ApplicationFontChange:
1564 d->label->setFont(f);
1571 QDialog::changeEvent(ev);
1575
1576
1577void QMessageBox::keyPressEvent(QKeyEvent *e)
1579#if QT_CONFIG(shortcut)
1581 if (e->matches(QKeySequence::Cancel)) {
1582 if (d->detectedEscapeButton) {
1584 d->detectedEscapeButton->animateClick();
1586 d->detectedEscapeButton->click();
1593#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
1595#if QT_CONFIG(textedit)
1596 if (e == QKeySequence::Copy) {
1597 if (d->detailsText && d->detailsText->isVisible() && d->detailsText->copy()) {
1598 e->setAccepted(
true);
1601 }
else if (e == QKeySequence::SelectAll && d->detailsText && d->detailsText->isVisible()) {
1602 d->detailsText->selectAll();
1603 e->setAccepted(
true);
1608#if defined(Q_OS_WIN)
1609 if (e == QKeySequence::Copy) {
1610 const auto separator =
"---------------------------\n"_L1;
1612 textToCopy += separator + windowTitle() + u'\n' + separator
1613 + d->label->text() + u'\n' + separator;
1615 if (d->informativeLabel)
1616 textToCopy += d->informativeLabel->text() + u'\n' + separator;
1618 const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
1619 for (
const auto *button : buttons)
1620 textToCopy += button->text() +
" "_L1;
1621 textToCopy += u'\n' + separator;
1622#if QT_CONFIG(textedit)
1624 textToCopy += d->detailsText->text() + u'\n' + separator;
1626 QGuiApplication::clipboard()->setText(textToCopy);
1633#ifndef QT_NO_SHORTCUT
1634 if (!(e->modifiers() & (Qt::AltModifier | Qt::ControlModifier | Qt::MetaModifier))) {
1635 int key = e->key() & ~Qt::MODIFIER_MASK;
1637 const QList<QAbstractButton *> buttons = d->buttonBox->buttons();
1638 for (
auto *pb : buttons) {
1639 QKeySequence shortcut = pb->shortcut();
1640 if (!shortcut.isEmpty() && key == shortcut[0].key()) {
1648 QDialog::keyPressEvent(e);
1652
1653
1654
1655
1656
1657
1658
1659void QMessageBox::open(QObject *receiver,
const char *member)
1662 const char *signal = member && strchr(member,
'*') ? SIGNAL(buttonClicked(QAbstractButton*))
1663 : SIGNAL(finished(
int));
1664 connect(
this, signal, receiver, member);
1665 d->signalToDisconnectOnClose = signal;
1666 d->receiverToDisconnectOnClose = receiver;
1667 d->memberToDisconnectOnClose = member;
1676 if (autoAddOkButton)
1677 q->addButton(QMessageBox::Ok);
1684 setNativeDialogVisible(visible);
1689 static_cast<QWidget*>(q_ptr)->setAttribute(Qt::WA_DontShowOnScreen, nativeDialogInUse);
1691 QDialogPrivate::setVisible(visible);
1695
1696
1697
1698
1699QList<QAbstractButton *> QMessageBox::buttons()
const
1701 Q_D(
const QMessageBox);
1702 return d->buttonBox->buttons();
1706
1707
1708
1709
1710
1711QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button)
const
1713 Q_D(
const QMessageBox);
1714 return QMessageBox::ButtonRole(d->buttonBox->buttonRole(button));
1718
1719
1720void QMessageBox::showEvent(QShowEvent *e)
1723 d->clickedButton =
nullptr;
1726#if QT_CONFIG(accessibility)
1727 QAccessibleEvent event(
this, QAccessible::Alert);
1728 QAccessible::updateAccessibility(&event);
1730#if defined(Q_OS_WIN)
1731 if (
const HMENU systemMenu = qt_getWindowsSystemMenu(
this)) {
1732 EnableMenuItem(systemMenu, SC_CLOSE, d->detectedEscapeButton ?
1733 MF_BYCOMMAND|MF_ENABLED : MF_BYCOMMAND|MF_GRAYED);
1736 QDialog::showEvent(e);
1741 QMessageBox::Icon icon,
1742 const QString& title,
const QString& text,
1743 QMessageBox::StandardButtons buttons,
1744 QMessageBox::StandardButton defaultButton)
1748 if (defaultButton && !(buttons & defaultButton)) {
1749 const int defaultButtons = defaultButton | QMessageBox::Default;
1750 const int otherButtons = buttons.toInt();
1751 const int ret = QMessageBoxPrivate::showOldMessageBox(parent, icon, title,
1754 return static_cast<QMessageBox::StandardButton>(ret);
1757 QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent);
1758 QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox*>();
1759 Q_ASSERT(buttonBox !=
nullptr);
1761 uint mask = QMessageBox::FirstButton;
1762 while (mask <= QMessageBox::LastButton) {
1763 uint sb = buttons & mask;
1767 QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb);
1769 if (msgBox.defaultButton())
1771 if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
1772 || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
1773 msgBox.setDefaultButton(button);
1775 if (msgBox.exec() == -1)
1776 return QMessageBox::Cancel;
1777 return msgBox.standardButton(msgBox.clickedButton());
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803QMessageBox::StandardButton QMessageBox::information(QWidget *parent,
const QString &title,
1804 const QString& text, StandardButtons buttons,
1805 StandardButton defaultButton)
1807 return showNewMessageBox(parent, Information, title, text, buttons,
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835QMessageBox::StandardButton QMessageBox::question(QWidget *parent,
const QString &title,
1836 const QString& text, StandardButtons buttons,
1837 StandardButton defaultButton)
1839 return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865QMessageBox::StandardButton QMessageBox::warning(QWidget *parent,
const QString &title,
1866 const QString& text, StandardButtons buttons,
1867 StandardButton defaultButton)
1869 return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895QMessageBox::StandardButton QMessageBox::critical(QWidget *parent,
const QString &title,
1896 const QString& text, StandardButtons buttons,
1897 StandardButton defaultButton)
1899 return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923void QMessageBox::about(QWidget *parent,
const QString &title,
const QString &text)
1926 static QPointer<QMessageBox> oldMsgBox;
1928 if (oldMsgBox && oldMsgBox->text() == text) {
1931 oldMsgBox->activateWindow();
1936 QMessageBox *msgBox =
new QMessageBox(Information, title, text, NoButton, parent
1938 , Qt::WindowTitleHint | Qt::WindowSystemMenuHint
1941 msgBox->setAttribute(Qt::WA_DeleteOnClose);
1942 QIcon icon = msgBox->windowIcon();
1943 msgBox->setIconPixmap(icon.pixmap(QSize(64, 64), msgBox->devicePixelRatio()));
1948 auto *d = msgBox->d_func();
1949 d->buttonBox->setCenterButtons(
true);
1951 msgBox->setModal(
true);
1953 msgBox->setModal(
false);
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976void QMessageBox::aboutQt(QWidget *parent,
const QString &title)
1979 static QPointer<QMessageBox> oldMsgBox;
1984 oldMsgBox->activateWindow();
1989 QString translatedTextAboutQtCaption;
1990 translatedTextAboutQtCaption = QMessageBox::tr(
1992 "<p>This program uses Qt version %1.</p>"
1993 ).arg(QT_VERSION_STR
""_L1);
1996 const QString translatedTextAboutQtText = QMessageBox::tr(
1997 "<p>Qt is a C++ toolkit for cross-platform application "
1999 "<p>Qt provides single-source portability across all major desktop "
2000 "operating systems. It is also available for embedded Linux and other "
2001 "embedded and mobile operating systems.</p>"
2002 "<p>Qt is available under multiple licensing options designed "
2003 "to accommodate the needs of our various users.</p>"
2004 "<p>Qt licensed under our commercial license agreement is appropriate "
2005 "for development of proprietary/commercial software where you do not "
2006 "want to share any source code with third parties or otherwise cannot "
2007 "comply with the terms of GNU (L)GPL.</p>"
2008 "<p>Qt licensed under GNU (L)GPL is appropriate for the "
2009 "development of Qt applications provided you can comply with the terms "
2010 "and conditions of the respective licenses.</p>"
2011 "<p>Please see <a href=\"https://%2/\">%2</a> "
2012 "for an overview of Qt licensing.</p>"
2013 "<p>Copyright (C) The Qt Company Ltd. and other "
2015 "<p>Qt and the Qt logo are trademarks of The Qt Company Ltd.</p>"
2016 "<p>Qt is The Qt Company Ltd. product developed as an open source "
2017 "project. See <a href=\"https://%3/\">%3</a> for more information.</p>"
2018 ).arg(QStringLiteral(
"qt.io/licensing"),
2019 QStringLiteral(
"qt.io"));
2020 QMessageBox *msgBox =
new QMessageBox(parent);
2021 msgBox->setAttribute(Qt::WA_DeleteOnClose);
2022 msgBox->setWindowTitle(title.isEmpty() ? tr(
"About Qt") : title);
2023 msgBox->setText(translatedTextAboutQtCaption);
2024 msgBox->setInformativeText(translatedTextAboutQtText);
2026 QPixmap pm(
":/qt-project.org/qmessagebox/images/qtlogo-64.png"_L1);
2028 msgBox->setIconPixmap(pm);
2033 auto *d = msgBox->d_func();
2034 d->buttonBox->setCenterButtons(
true);
2036 msgBox->setModal(
true);
2038 msgBox->setModal(
false);
2052 if (button == QMessageBox::NoButton || (button & NewButtonMask))
2053 return QMessageBox::StandardButton(button & QMessageBox::ButtonMask);
2055 return QMessageBox::NoButton;
2074 if (button0 & flags) {
2076 }
else if (button1 & flags) {
2078 }
else if (button2 & flags) {
2081 return q->button(newButton(button));
2087 q->addButton(newButton(button0));
2088 q->addButton(newButton(button1));
2089 q->addButton(newButton(button2));
2090 q->setDefaultButton(
2091 static_cast<QPushButton *>(findButton(button0, button1, button2, QMessageBox::Default)));
2092 q->setEscapeButton(findButton(button0, button1, button2, QMessageBox::Escape));
2098 Q_Q(
const QMessageBox);
2099 QAbstractButton *result = customButtonList.value(id);
2102 if (id & QMessageBox::FlagMask)
2104 return q->button(newButton(id));
2108 const QString &title,
const QString &text,
2109 int button0,
int button1,
int button2)
2111 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2112 messageBox.d_func()->addOldButtons(button0, button1, button2);
2113 return messageBox.exec();
2117 const QString &title,
const QString &text,
2118 const QString &button0Text,
2119 const QString &button1Text,
2120 const QString &button2Text,
2121 int defaultButtonNumber,
2122 int escapeButtonNumber)
2124 QMessageBox messageBox(icon, title, text, QMessageBox::NoButton, parent);
2125 QString myButton0Text = button0Text;
2126 if (myButton0Text.isEmpty())
2127 myButton0Text = QDialogButtonBox::tr(
"OK");
2128 messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
2129 if (!button1Text.isEmpty())
2130 messageBox.addButton(button1Text, QMessageBox::ActionRole);
2131 if (!button2Text.isEmpty())
2132 messageBox.addButton(button2Text, QMessageBox::ActionRole);
2134 const QList<QAbstractButton *> &buttonList = messageBox.d_func()->customButtonList;
2135 messageBox.setDefaultButton(
static_cast<QPushButton *>(buttonList.value(defaultButtonNumber)));
2136 messageBox.setEscapeButton(buttonList.value(escapeButtonNumber));
2142 return buttonList.indexOf(messageBox.clickedButton());
2147#if QT_CONFIG(textedit)
2148 if (detailsButton && detailsText)
2149 detailsButton->setLabel(detailsText->isHidden() ? ShowLabel : HideLabel);
2153#if QT_DEPRECATED_SINCE(6
,2
)
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
2200
2201
2202
2203
2204
2205
2206QMessageBox::QMessageBox(
const QString &title,
const QString &text, Icon icon,
2207 int button0,
int button1,
int button2, QWidget *parent,
2209 : QDialog(*
new QMessageBoxPrivate, parent,
2210 f | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
2213 d->init(title, text);
2215 d->addOldButtons(button0, button1, button2);
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
2251
2252
2253
2254
2255
2256
2257int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2258 int button0,
int button1,
int button2)
2260 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2261 button0, button1, button2);
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
2294int QMessageBox::information(QWidget *parent,
const QString &title,
const QString& text,
2295 const QString& button0Text,
const QString& button1Text,
2296 const QString& button2Text,
int defaultButtonNumber,
2297 int escapeButtonNumber)
2299 return QMessageBoxPrivate::showOldMessageBox(parent, Information, title, text,
2300 button0Text, button1Text, button2Text,
2301 defaultButtonNumber, escapeButtonNumber);
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
2338
2339
2340
2341
2342
2343
2344int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2345 int button0,
int button1,
int button2)
2347 return QMessageBoxPrivate::showOldMessageBox(parent, Question, 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
2380int QMessageBox::question(QWidget *parent,
const QString &title,
const QString& text,
2381 const QString& button0Text,
const QString& button1Text,
2382 const QString& button2Text,
int defaultButtonNumber,
2383 int escapeButtonNumber)
2385 return QMessageBoxPrivate::showOldMessageBox(parent, Question, title, text,
2386 button0Text, button1Text, button2Text,
2387 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
2430int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2431 int button0,
int button1,
int button2)
2433 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2434 button0, button1, button2);
2438
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
2466int QMessageBox::warning(QWidget *parent,
const QString &title,
const QString& text,
2467 const QString& button0Text,
const QString& button1Text,
2468 const QString& button2Text,
int defaultButtonNumber,
2469 int escapeButtonNumber)
2471 return QMessageBoxPrivate::showOldMessageBox(parent, Warning, title, text,
2472 button0Text, button1Text, button2Text,
2473 defaultButtonNumber, escapeButtonNumber);
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
2509
2510
2511
2512
2513
2514
2516int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2517 int button0,
int button1,
int button2)
2519 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2520 button0, button1, button2);
2524
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
2552int QMessageBox::critical(QWidget *parent,
const QString &title,
const QString& text,
2553 const QString& button0Text,
const QString& button1Text,
2554 const QString& button2Text,
int defaultButtonNumber,
2555 int escapeButtonNumber)
2557 return QMessageBoxPrivate::showOldMessageBox(parent, Critical, title, text,
2558 button0Text, button1Text, button2Text,
2559 defaultButtonNumber, escapeButtonNumber);
2564
2565
2566
2567
2568
2569
2570
2571QString QMessageBox::buttonText(
int button)
const
2573 Q_D(
const QMessageBox);
2575 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2576 return abstractButton->text();
2577 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2579 return QDialogButtonBox::tr(
"OK");
2585
2586
2587
2588
2589
2590
2591
2592
2593void QMessageBox::setButtonText(
int button,
const QString &text)
2596 if (QAbstractButton *abstractButton = d->abstractButtonForId(button)) {
2597 abstractButton->setText(text);
2598 }
else if (d->buttonBox->buttons().isEmpty() && (button == Ok || button == Old_Ok)) {
2600 addButton(QMessageBox::Ok)->setText(text);
2606#if QT_CONFIG(textedit)
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617QString QMessageBox::detailedText()
const
2619 Q_D(
const QMessageBox);
2620 return d->detailsText ? d->detailsText->text() : QString();
2623void QMessageBox::setDetailedText(
const QString &text)
2626 if (text.isEmpty()) {
2627 if (d->detailsText) {
2628 d->detailsText->hide();
2629 d->detailsText->deleteLater();
2631 d->detailsText =
nullptr;
2632 removeButton(d->detailsButton);
2633 if (d->detailsButton) {
2634 d->detailsButton->hide();
2635 d->detailsButton->deleteLater();
2637 d->detailsButton =
nullptr;
2639 if (!d->detailsText) {
2640 d->detailsText =
new QMessageBoxDetailsText(
this);
2641 d->detailsText->hide();
2643 if (!d->detailsButton) {
2644 const bool autoAddOkButton = d->autoAddOkButton;
2645 d->detailsButton =
new DetailButton(
this);
2646 addButton(d->detailsButton, QMessageBox::ActionRole);
2647 d->autoAddOkButton = autoAddOkButton;
2649 d->detailsText->setText(text);
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674QString QMessageBox::informativeText()
const
2676 Q_D(
const QMessageBox);
2677 return d->informativeLabel ? d->informativeLabel->text() : QString();
2680void QMessageBox::setInformativeText(
const QString &text)
2683 if (text.isEmpty()) {
2684 if (d->informativeLabel) {
2685 d->informativeLabel->hide();
2686 d->informativeLabel->deleteLater();
2688 d->informativeLabel =
nullptr;
2690 if (!d->informativeLabel) {
2691 QLabel *label =
new QLabel;
2692 label->setObjectName(
"qt_msgbox_informativelabel"_L1);
2693 label->setTextInteractionFlags(Qt::TextInteractionFlags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags,
nullptr,
this)));
2694 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
2695 label->setOpenExternalLinks(
true);
2698 label->setFont(qt_app_fonts_hash()->value(
"QTipLabel"));
2700 label->setWordWrap(
true);
2701 label->setTextFormat(d->label->textFormat());
2702 d->informativeLabel = label;
2704 d->informativeLabel->setText(text);
2710
2711
2712
2713
2714
2715
2716void QMessageBox::setWindowTitle(
const QString &title)
2720 QDialog::setWindowTitle(title);
2728
2729
2730
2731
2732
2733
2734
2735
2736void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
2738 QDialog::setWindowModality(windowModality);
2740 if (parentWidget() && windowModality == Qt::WindowModal)
2741 setParent(parentWidget(), Qt::Sheet);
2743 setParent(parentWidget(), Qt::Dialog);
2744 setDefaultButton(d_func()->defaultButton);
2750 QStyle *style = mb ? mb->style() : QApplication::style();
2751 int iconSize = style->pixelMetric(QStyle::PM_MessageBoxIconSize,
nullptr, mb);
2754 case QMessageBox::Information:
2755 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxInformation,
nullptr, mb);
2757 case QMessageBox::Warning:
2758 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxWarning,
nullptr, mb);
2760 case QMessageBox::Critical:
2761 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxCritical,
nullptr, mb);
2763 case QMessageBox::Question:
2764 tmpIcon = style->standardIcon(QStyle::SP_MessageBoxQuestion,
nullptr, mb);
2769 if (!tmpIcon.isNull()) {
2770 qreal dpr = mb ? mb->devicePixelRatio() : qApp->devicePixelRatio();
2771 return tmpIcon.pixmap(QSize(iconSize, iconSize), dpr);
2778 auto *messageDialogHelper =
static_cast<QPlatformMessageDialogHelper *>(h);
2779 QObjectPrivate::connect(messageDialogHelper, &QPlatformMessageDialogHelper::clicked,
2780 this, &QMessageBoxPrivate::helperClicked);
2783 QObject::connect(messageDialogHelper, &QPlatformMessageDialogHelper::checkBoxStateChanged,
2784 q_ptr, [
this](Qt::CheckState state) {
2786 checkbox->setCheckState(state);
2789 messageDialogHelper->setOptions(options);
2795 case QMessageBox::NoIcon:
2796 return QMessageDialogOptions::NoIcon;
2797 case QMessageBox::Information:
2798 return QMessageDialogOptions::Information;
2799 case QMessageBox::Warning:
2800 return QMessageDialogOptions::Warning;
2801 case QMessageBox::Critical:
2802 return QMessageDialogOptions::Critical;
2803 case QMessageBox::Question:
2804 return QMessageDialogOptions::Question;
2806 return QMessageDialogOptions::NoIcon;
2811 QPlatformDialogHelper::StandardButtons buttons(
int(q->standardButtons()));
2819 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
2820 if (nativeDialogInUse)
2822 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
2823 || q->testAttribute(Qt::WA_DontShowOnScreen)
2824 || q->testAttribute(Qt::WA_StyleSheet)
2825 || (options->options() & QMessageDialogOptions::Option::DontUseNativeDialog)) {
2829 if (strcmp(QMessageBox::staticMetaObject.className(), q->metaObject()->className()) != 0)
2833 for (
auto *customButton : buttonBox->buttons()) {
2834 if (QPushButton *pushButton = qobject_cast<QPushButton *>(customButton)) {
2836 if (pushButton->menu())
2842 return QDialogPrivate::canBeNativeDialog();
2848 options->setWindowTitle(q->windowTitle());
2849 options->setText(q->text());
2850 options->setInformativeText(q->informativeText());
2851#if QT_CONFIG(textedit)
2852 options->setDetailedText(q->detailedText());
2854 options->setStandardIcon(helperIcon(q->icon()));
2855 options->setIconPixmap(q->iconPixmap());
2858 options->clearCustomButtons();
2861 auto standardButtons = helperStandardButtons(q);
2862 for (
int button = QDialogButtonBox::StandardButton::FirstButton;
2863 button <= QDialogButtonBox::StandardButton::LastButton; button <<= 1) {
2864 auto *standardButton = buttonBox->button(QDialogButtonBox::StandardButton(button));
2865 if (!standardButton)
2868 if (
auto *platformTheme = QGuiApplicationPrivate::platformTheme()) {
2869 if (standardButton->text() != platformTheme->standardButtonText(button)) {
2872 const auto buttonRole = buttonBox->buttonRole(standardButton);
2873 options->addButton(standardButton->text(),
2874 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2875 standardButton, button);
2876 standardButtons &= ~QPlatformDialogHelper::StandardButton(button);
2880 if (standardButton == defaultButton)
2881 options->setDefaultButton(button);
2882 else if (standardButton == detectedEscapeButton)
2883 options->setEscapeButton(button);
2885 options->setStandardButtons(standardButtons);
2888 for (
auto *customButton : customButtonList) {
2891 if (customButton == detailsButton)
2894 const auto buttonRole = buttonBox->buttonRole(customButton);
2895 const int buttonId = options->addButton(customButton->text(),
2896 static_cast<QPlatformDialogHelper::ButtonRole>(buttonRole),
2899 if (customButton == defaultButton)
2900 options->setDefaultButton(buttonId);
2901 else if (customButton == detectedEscapeButton)
2902 options->setEscapeButton(buttonId);
2906 options->setCheckBox(checkbox->text(), checkbox->checkState());
2911 const auto required = QVersionNumber::fromString(req).normalized();
2912 const auto current = QVersionNumber::fromString(qVersion()).normalized();
2913 if (current >= required)
2915 std::optional<QApplication> application;
2917 application.emplace(argc, argv);
2918 const QString message = QApplication::tr(
"Application \"%1\" requires Qt %2, found Qt %3.")
2919 .arg(qAppName(), required.toString(), current.toString());
2920 QMessageBox::critical(
nullptr, QApplication::tr(
"Incompatible Qt Library Error"),
2921 message, QMessageBox::Abort);
2922 qFatal(
"%ls", qUtf16Printable(message));
2925#if QT_DEPRECATED_SINCE(6
,2
)
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2939QPixmap QMessageBox::standardIcon(Icon icon)
2941 return QMessageBoxPrivate::standardIcon(icon,
nullptr);
2946
2947
2948
2949
2950
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3012#include "moc_qmessagebox.cpp"
3013#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)