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 <jpnurmi@digia.com>
This commit is contained in:
Thorbjørn Martsum 2013-06-10 07:30:01 +02:00 committed by The Qt Project
parent 16c47c3b34
commit 3e87d7e814
4 changed files with 83 additions and 3 deletions

View File

@ -53,6 +53,7 @@
#include <QtWidgets/qgridlayout.h> #include <QtWidgets/qgridlayout.h>
#include <QtWidgets/qdesktopwidget.h> #include <QtWidgets/qdesktopwidget.h>
#include <QtWidgets/qpushbutton.h> #include <QtWidgets/qpushbutton.h>
#include <QtWidgets/qcheckbox.h>
#include <QtGui/qaccessible.h> #include <QtGui/qaccessible.h>
#include <QtGui/qicon.h> #include <QtGui/qicon.h>
#include <QtGui/qtextdocument.h> #include <QtGui/qtextdocument.h>
@ -198,7 +199,7 @@ class QMessageBoxPrivate : public QDialogPrivate
Q_DECLARE_PUBLIC(QMessageBox) Q_DECLARE_PUBLIC(QMessageBox)
public: 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 #ifndef QT_NO_TEXTEDIT
detailsText(0), detailsText(0),
#endif #endif
@ -248,6 +249,7 @@ public:
QList<QAbstractButton *> customButtonList; QList<QAbstractButton *> customButtonList;
QAbstractButton *escapeButton; QAbstractButton *escapeButton;
QPushButton *defaultButton; QPushButton *defaultButton;
QCheckBox *checkbox;
QAbstractButton *clickedButton; QAbstractButton *clickedButton;
DetailButton *detailsButton; DetailButton *detailsButton;
#ifndef QT_NO_TEXTEDIT #ifndef QT_NO_TEXTEDIT
@ -317,8 +319,16 @@ void QMessageBoxPrivate::setupLayout()
#endif #endif
grid->addWidget(informativeLabel, 1, hasIcon ? 2 : 1, 1, 1); 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 #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->setMargin(0);
grid->setVerticalSpacing(8); grid->setVerticalSpacing(8);
grid->setHorizontalSpacing(0); grid->setHorizontalSpacing(0);
@ -326,7 +336,7 @@ void QMessageBoxPrivate::setupLayout()
grid->setRowStretch(1, 100); grid->setRowStretch(1, 100);
grid->setRowMinimumHeight(2, 6); grid->setRowMinimumHeight(2, 6);
#else #else
grid->addWidget(buttonBox, 2, 0, 1, grid->columnCount()); grid->addWidget(buttonBox, grid->rowCount(), 0, 1, grid->columnCount());
#endif #endif
if (detailsText) if (detailsText)
grid->addWidget(detailsText, grid->rowCount(), 0, 1, grid->columnCount()); 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))); 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 \property QMessageBox::text
\brief the message box text to be displayed. \brief the message box text to be displayed.

View File

@ -52,6 +52,7 @@ QT_BEGIN_NAMESPACE
class QLabel; class QLabel;
class QMessageBoxPrivate; class QMessageBoxPrivate;
class QAbstractButton; class QAbstractButton;
class QCheckBox;
class Q_WIDGETS_EXPORT QMessageBox : public QDialog class Q_WIDGETS_EXPORT QMessageBox : public QDialog
{ {
@ -188,6 +189,9 @@ public:
void setTextInteractionFlags(Qt::TextInteractionFlags flags); void setTextInteractionFlags(Qt::TextInteractionFlags flags);
Qt::TextInteractionFlags textInteractionFlags() const; Qt::TextInteractionFlags textInteractionFlags() const;
void setCheckBox(QCheckBox *cb);
QCheckBox* checkBox() const;
static StandardButton information(QWidget *parent, const QString &title, static StandardButton information(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok, const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton); StandardButton defaultButton = NoButton);

View File

@ -63,6 +63,8 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent)
,m_btnShowApply(new QPushButton) ,m_btnShowApply(new QPushButton)
,m_resultLabel(new QLabel) ,m_resultLabel(new QLabel)
,m_chkReallocMsgBox(new QCheckBox(QString::fromLatin1("Reallocate Message Box"))) ,m_chkReallocMsgBox(new QCheckBox(QString::fromLatin1("Reallocate Message Box")))
,m_checkboxText(new QLineEdit)
,m_checkBoxResult(new QLabel)
,m_msgbox(new QMessageBox) ,m_msgbox(new QMessageBox)
{ {
// --- Options --- // --- Options ---
@ -97,6 +99,10 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent)
m_buttonsMask->setText(QString::fromLatin1("0x00300400")); m_buttonsMask->setText(QString::fromLatin1("0x00300400"));
optionsLayout->addWidget(m_buttonsMask); optionsLayout->addWidget(m_buttonsMask);
// check box check
optionsLayout->addWidget(new QLabel(QString::fromLatin1("Checkbox text ("" => no chkbox)")));
optionsLayout->addWidget(m_checkboxText);
// reallocate // reallocate
optionsLayout->addWidget(m_chkReallocMsgBox); optionsLayout->addWidget(m_chkReallocMsgBox);
optionsLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding)); optionsLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
@ -114,6 +120,7 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent)
// result label // result label
execLayout->addWidget(m_resultLabel); execLayout->addWidget(m_resultLabel);
execLayout->addWidget(m_checkBoxResult);
execLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding)); execLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding));
execGroupBox->setLayout(execLayout); execGroupBox->setLayout(execLayout);
@ -129,6 +136,7 @@ MessageBoxPanel::MessageBoxPanel(QWidget *parent) : QWidget(parent)
void MessageBoxPanel::setupMessageBox(QMessageBox &box) void MessageBoxPanel::setupMessageBox(QMessageBox &box)
{ {
m_resultLabel->setText(QString()); m_resultLabel->setText(QString());
m_checkBoxResult->setText(QString());
box.setText(m_textInMsgBox->text()); box.setText(m_textInMsgBox->text());
box.setInformativeText(m_informativeText->text()); box.setInformativeText(m_informativeText->text());
box.setDetailedText(m_detailedtext->text()); box.setDetailedText(m_detailedtext->text());
@ -141,6 +149,10 @@ void MessageBoxPanel::setupMessageBox(QMessageBox &box)
if (box.standardButtons() == (QMessageBox::StandardButtons) 0) if (box.standardButtons() == (QMessageBox::StandardButtons) 0)
box.setStandardButtons(QMessageBox::Ok); // just to have something. 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()); box.setIcon((QMessageBox::Icon) m_iconComboBox->currentIndex());
} }
@ -164,6 +176,12 @@ void MessageBoxPanel::doExec()
QString sres; QString sres;
sres.setNum(res, 16); sres.setNum(res, 16);
m_resultLabel->setText(QString::fromLatin1("Return value (hex): %1").arg(sres)); 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() void MessageBoxPanel::doShowApply()

View File

@ -43,6 +43,7 @@
#define MESSAGEBOXPANEL_H #define MESSAGEBOXPANEL_H
#include <QWidget> #include <QWidget>
#include <QCheckBox>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QComboBox; class QComboBox;
@ -77,6 +78,8 @@ private:
QValidator *m_validator; QValidator *m_validator;
QLabel *m_resultLabel; QLabel *m_resultLabel;
QCheckBox *m_chkReallocMsgBox; QCheckBox *m_chkReallocMsgBox;
QLineEdit *m_checkboxText;
QLabel *m_checkBoxResult;
QMessageBox *m_msgbox; QMessageBox *m_msgbox;
void setupMessageBox(QMessageBox &box); void setupMessageBox(QMessageBox &box);
}; };