From 3e87d7e81458a8622ae461ed03d881a17aade41c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Martsum?= Date: Mon, 10 Jun 2013 07:30:01 +0200 Subject: QMessageBox - make it possible to have a checkbox on the dialog This (partly) solves Task-number: QTBUG-2450 Change-Id: Ie2280c87b96e72acc76e806a83c4e8cc0d4e4ee4 Reviewed-by: J-P Nurmi --- src/widgets/dialogs/qmessagebox.cpp | 61 ++++++++++++++++++++++++++++++-- src/widgets/dialogs/qmessagebox.h | 4 +++ tests/manual/dialogs/messageboxpanel.cpp | 18 ++++++++++ tests/manual/dialogs/messageboxpanel.h | 3 ++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index f545d1ffd2..da5ec8dc8a 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -198,7 +199,7 @@ class QMessageBoxPrivate : public QDialogPrivate Q_DECLARE_PUBLIC(QMessageBox) public: - QMessageBoxPrivate() : escapeButton(0), defaultButton(0), clickedButton(0), detailsButton(0), + QMessageBoxPrivate() : escapeButton(0), defaultButton(0), checkbox(0), clickedButton(0), detailsButton(0), #ifndef QT_NO_TEXTEDIT detailsText(0), #endif @@ -248,6 +249,7 @@ public: QList customButtonList; QAbstractButton *escapeButton; QPushButton *defaultButton; + QCheckBox *checkbox; QAbstractButton *clickedButton; DetailButton *detailsButton; #ifndef QT_NO_TEXTEDIT @@ -317,8 +319,16 @@ void QMessageBoxPrivate::setupLayout() #endif grid->addWidget(informativeLabel, 1, hasIcon ? 2 : 1, 1, 1); } + if (checkbox) { + grid->addWidget(checkbox, informativeLabel ? 2 : 1, hasIcon ? 2 : 1, 1, 1, Qt::AlignLeft); #ifdef Q_OS_MAC - grid->addWidget(buttonBox, 3, hasIcon ? 2 : 1, 1, 1); + grid->addItem(new QSpacerItem(1, 15, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0); +#else + grid->addItem(new QSpacerItem(1, 7, QSizePolicy::Fixed, QSizePolicy::Fixed), grid->rowCount(), 0); +#endif + } +#ifdef Q_OS_MAC + grid->addWidget(buttonBox, grid->rowCount(), hasIcon ? 2 : 1, 1, 1); grid->setMargin(0); grid->setVerticalSpacing(8); grid->setHorizontalSpacing(0); @@ -326,7 +336,7 @@ void QMessageBoxPrivate::setupLayout() grid->setRowStretch(1, 100); grid->setRowMinimumHeight(2, 6); #else - grid->addWidget(buttonBox, 2, 0, 1, grid->columnCount()); + grid->addWidget(buttonBox, grid->rowCount(), 0, 1, grid->columnCount()); #endif if (detailsText) grid->addWidget(detailsText, grid->rowCount(), 0, 1, grid->columnCount()); @@ -1127,6 +1137,51 @@ void QMessageBox::setDefaultButton(QMessageBox::StandardButton button) setDefaultButton(d->buttonBox->button(QDialogButtonBox::StandardButton(button))); } +/*! \since 5.2 + + Sets the checkbox \a cb on the message dialog. The message box takes ownership of the checkbox. + The argument \a cb can be 0 to remove an existing checkbox from the message box. + + \sa checkBox() +*/ + +void QMessageBox::setCheckBox(QCheckBox *cb) +{ + Q_D(QMessageBox); + + if (cb == d->checkbox) + return; + + if (d->checkbox) { + d->checkbox->hide(); + layout()->removeWidget(d->checkbox); + if (d->checkbox->parentWidget() == this) { + d->checkbox->setParent(0); + d->checkbox->deleteLater(); + } + } + d->checkbox = cb; + if (d->checkbox) { + QSizePolicy sp = d->checkbox->sizePolicy(); + sp.setHorizontalPolicy(QSizePolicy::MinimumExpanding); + d->checkbox->setSizePolicy(sp); + } + d->setupLayout(); +} + + +/*! \since 5.2 + + Returns the checkbox shown on the dialog. This is 0 if no checkbox is set. + \sa setCheckBox() +*/ + +QCheckBox* QMessageBox::checkBox() const +{ + Q_D(const QMessageBox); + return d->checkbox; +} + /*! \property QMessageBox::text \brief the message box text to be displayed. diff --git a/src/widgets/dialogs/qmessagebox.h b/src/widgets/dialogs/qmessagebox.h index c7c42b1e7a..58be13426c 100644 --- a/src/widgets/dialogs/qmessagebox.h +++ b/src/widgets/dialogs/qmessagebox.h @@ -52,6 +52,7 @@ QT_BEGIN_NAMESPACE class QLabel; class QMessageBoxPrivate; class QAbstractButton; +class QCheckBox; class Q_WIDGETS_EXPORT QMessageBox : public QDialog { @@ -188,6 +189,9 @@ public: void setTextInteractionFlags(Qt::TextInteractionFlags flags); Qt::TextInteractionFlags textInteractionFlags() const; + void setCheckBox(QCheckBox *cb); + QCheckBox* checkBox() const; + static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton); diff --git a/tests/manual/dialogs/messageboxpanel.cpp b/tests/manual/dialogs/messageboxpanel.cpp index 6fc84e11fd..529a8251e1 100644 --- a/tests/manual/dialogs/messageboxpanel.cpp +++ b/tests/manual/dialogs/messageboxpanel.cpp @@ -63,6 +63,8 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent) ,m_btnShowApply(new QPushButton) ,m_resultLabel(new QLabel) ,m_chkReallocMsgBox(new QCheckBox(QString::fromLatin1("Reallocate Message Box"))) +,m_checkboxText(new QLineEdit) +,m_checkBoxResult(new QLabel) ,m_msgbox(new QMessageBox) { // --- Options --- @@ -97,6 +99,10 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent) m_buttonsMask->setText(QString::fromLatin1("0x00300400")); optionsLayout->addWidget(m_buttonsMask); + // check box check + optionsLayout->addWidget(new QLabel(QString::fromLatin1("Checkbox text ("" => no chkbox)"))); + optionsLayout->addWidget(m_checkboxText); + // reallocate optionsLayout->addWidget(m_chkReallocMsgBox); optionsLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding)); @@ -114,6 +120,7 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent) // result label execLayout->addWidget(m_resultLabel); + execLayout->addWidget(m_checkBoxResult); execLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding)); execGroupBox->setLayout(execLayout); @@ -129,6 +136,7 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent) void MessageBoxPanel::setupMessageBox(QMessageBox &box) { m_resultLabel->setText(QString()); + m_checkBoxResult->setText(QString()); box.setText(m_textInMsgBox->text()); box.setInformativeText(m_informativeText->text()); box.setDetailedText(m_detailedtext->text()); @@ -141,6 +149,10 @@ void MessageBoxPanel::setupMessageBox(QMessageBox &box) if (box.standardButtons() == (QMessageBox::StandardButtons) 0) box.setStandardButtons(QMessageBox::Ok); // just to have something. + box.setCheckBox(0); + if (m_checkboxText->text().length() > 0) + box.setCheckBox(new QCheckBox(m_checkboxText->text())); + box.setIcon((QMessageBox::Icon) m_iconComboBox->currentIndex()); } @@ -164,6 +176,12 @@ void MessageBoxPanel::doExec() QString sres; sres.setNum(res, 16); m_resultLabel->setText(QString::fromLatin1("Return value (hex): %1").arg(sres)); + if (m_msgbox->checkBox()) { + if (m_msgbox->checkBox()->isChecked()) + m_checkBoxResult->setText(QString::fromLatin1("Checkbox was checked")); + else + m_checkBoxResult->setText(QString::fromLatin1("Checkbox was not checked")); + } } void MessageBoxPanel::doShowApply() diff --git a/tests/manual/dialogs/messageboxpanel.h b/tests/manual/dialogs/messageboxpanel.h index 9f7e35210c..138007c244 100644 --- a/tests/manual/dialogs/messageboxpanel.h +++ b/tests/manual/dialogs/messageboxpanel.h @@ -43,6 +43,7 @@ #define MESSAGEBOXPANEL_H #include +#include QT_BEGIN_NAMESPACE class QComboBox; @@ -77,6 +78,8 @@ private: QValidator *m_validator; QLabel *m_resultLabel; QCheckBox *m_chkReallocMsgBox; + QLineEdit *m_checkboxText; + QLabel *m_checkBoxResult; QMessageBox *m_msgbox; void setupMessageBox(QMessageBox &box); }; -- cgit v1.2.3