Add getMultiLineText static method to QInputDialog

Change-Id: I6d43772f6ee66c6c43d64fb3d18f2a5f0c894a00
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
David Gil 2013-05-22 11:59:39 +02:00 committed by The Qt Project
parent 7ed024226f
commit cc54161abb
4 changed files with 131 additions and 12 deletions

View File

@ -121,6 +121,10 @@ Dialog::Dialog(QWidget *parent)
textLabel->setFrameStyle(frameStyle);
QPushButton *textButton = new QPushButton(tr("QInputDialog::get&Text()"));
multiLineTextLabel = new QLabel;
multiLineTextLabel->setFrameStyle(frameStyle);
QPushButton *multiLineTextButton = new QPushButton(tr("QInputDialog::get&MultiLineText()"));
colorLabel = new QLabel;
colorLabel->setFrameStyle(frameStyle);
QPushButton *colorButton = new QPushButton(tr("QColorDialog::get&Color()"));
@ -177,6 +181,7 @@ Dialog::Dialog(QWidget *parent)
connect(doubleButton, SIGNAL(clicked()), this, SLOT(setDouble()));
connect(itemButton, SIGNAL(clicked()), this, SLOT(setItem()));
connect(textButton, SIGNAL(clicked()), this, SLOT(setText()));
connect(multiLineTextButton, SIGNAL(clicked()), this, SLOT(setMultiLineText()));
connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
connect(fontButton, SIGNAL(clicked()), this, SLOT(setFont()));
connect(directoryButton, SIGNAL(clicked()),
@ -206,7 +211,9 @@ Dialog::Dialog(QWidget *parent)
layout->addWidget(itemLabel, 2, 1);
layout->addWidget(textButton, 3, 0);
layout->addWidget(textLabel, 3, 1);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 4, 0);
layout->addWidget(multiLineTextButton, 4, 0);
layout->addWidget(multiLineTextLabel, 4, 1);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding), 5, 0);
toolbox->addItem(page, tr("Input Dialogs"));
const QString doNotUseNativeDialog = tr("Do not use native dialog");
@ -327,6 +334,17 @@ void Dialog::setText()
//! [3]
}
void Dialog::setMultiLineText()
{
//! [4]
bool ok;
QString text = QInputDialog::getMultiLineText(this, tr("QInputDialog::getMultiLineText()"),
tr("Address:"), "John Doe\nFreedom Street", &ok);
if (ok && !text.isEmpty())
multiLineTextLabel->setText(text);
//! [4]
}
void Dialog::setColor()
{
const QColorDialog::ColorDialogOptions options = QFlag(colorDialogOptionsWidget->value());

View File

@ -63,6 +63,7 @@ private slots:
void setDouble();
void setItem();
void setText();
void setMultiLineText();
void setColor();
void setFont();
void setExistingDirectory();
@ -80,6 +81,7 @@ private:
QLabel *doubleLabel;
QLabel *itemLabel;
QLabel *textLabel;
QLabel *multiLineTextLabel;
QLabel *colorLabel;
QLabel *fontLabel;
QLabel *directoryLabel;

View File

@ -49,6 +49,7 @@
#include "qlabel.h"
#include "qlayout.h"
#include "qlineedit.h"
#include "qplaintextedit.h"
#include "qlistwidget.h"
#include "qpushbutton.h"
#include "qspinbox.h"
@ -167,6 +168,7 @@ public:
void ensureLayout();
void ensureLineEdit();
void ensurePlainTextEdit();
void ensureComboBox();
void ensureListView();
void ensureIntSpinBox();
@ -180,11 +182,13 @@ public:
void ensureLayout() const { const_cast<QInputDialogPrivate *>(this)->ensureLayout(); }
bool useComboBoxOrListView() const { return comboBox && comboBox->count() > 0; }
void _q_textChanged(const QString &text);
void _q_plainTextEditTextChanged();
void _q_currentRowChanged(const QModelIndex &newIndex, const QModelIndex &oldIndex);
mutable QLabel *label;
mutable QDialogButtonBox *buttonBox;
mutable QLineEdit *lineEdit;
mutable QPlainTextEdit *plainTextEdit;
mutable QSpinBox *intSpinBox;
mutable QDoubleSpinBox *doubleSpinBox;
mutable QComboBox *comboBox;
@ -198,8 +202,8 @@ public:
};
QInputDialogPrivate::QInputDialogPrivate()
: label(0), buttonBox(0), lineEdit(0), intSpinBox(0), doubleSpinBox(0), comboBox(0), listView(0),
inputWidget(0), mainLayout(0)
: label(0), buttonBox(0), lineEdit(0), plainTextEdit(0), intSpinBox(0), doubleSpinBox(0),
comboBox(0), listView(0), inputWidget(0), mainLayout(0)
{
}
@ -250,6 +254,21 @@ void QInputDialogPrivate::ensureLineEdit()
}
}
void QInputDialogPrivate::ensurePlainTextEdit()
{
Q_Q(QInputDialog);
if (!plainTextEdit) {
plainTextEdit = new QPlainTextEdit(q);
plainTextEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
#ifndef QT_NO_IM
qt_widget_private(plainTextEdit)->inheritsInputMethodHints = 1;
#endif
plainTextEdit->hide();
QObject::connect(plainTextEdit, SIGNAL(textChanged()),
q, SLOT(_q_plainTextEditTextChanged()));
}
}
void QInputDialogPrivate::ensureComboBox()
{
Q_Q(QInputDialog);
@ -344,6 +363,8 @@ void QInputDialogPrivate::setInputWidget(QWidget *widget)
// textValue
if (widget == lineEdit) {
lineEdit->setText(textValue);
} else if (widget == plainTextEdit) {
plainTextEdit->setPlainText(textValue);
} else if (widget == comboBox) {
setComboBoxText(textValue);
} else if (widget == listView) {
@ -364,6 +385,9 @@ void QInputDialogPrivate::chooseRightTextInputWidget()
} else {
widget = comboBox;
}
} else if (opts & QInputDialog::UsePlainTextEditForTextInput) {
ensurePlainTextEdit();
widget = plainTextEdit;
} else {
ensureLineEdit();
widget = lineEdit;
@ -417,6 +441,16 @@ void QInputDialogPrivate::_q_textChanged(const QString &text)
}
}
void QInputDialogPrivate::_q_plainTextEditTextChanged()
{
Q_Q(QInputDialog);
QString text = plainTextEdit->toPlainText();
if (textValue != text) {
textValue = text;
emit q->textValueChanged(text);
}
}
void QInputDialogPrivate::_q_currentRowChanged(const QModelIndex &newIndex,
const QModelIndex & /* oldIndex */)
{
@ -434,8 +468,8 @@ void QInputDialogPrivate::_q_currentRowChanged(const QModelIndex &newIndex,
The input value can be a string, a number or an item from a list. A label
must be set to tell the user what they should enter.
Four static convenience functions are provided: getText(), getInt(),
getDouble(), and getItem(). All the functions can be used in a similar way,
Five static convenience functions are provided: getText(), getMultiLineText(),
getInt(), getDouble(), and getItem(). All the functions can be used in a similar way,
for example:
\snippet dialogs/standarddialogs/dialog.cpp 3
@ -575,6 +609,8 @@ QString QInputDialog::labelText() const
\value NoButtons Don't display \uicontrol{OK} and \uicontrol{Cancel} buttons (useful for "live dialogs").
\value UseListViewForComboBoxItems Use a QListView rather than a non-editable QComboBox for
displaying the items set with setComboBoxItems().
\value UsePlainTextEditForTextInput Use a QPlainTextEdit for multiline text input. This value was
introduced in 5.2.
\sa options, setOption(), testOption()
*/
@ -628,6 +664,8 @@ void QInputDialog::setOptions(InputDialogOptions options)
d->buttonBox->setVisible(!(options & NoButtons));
if ((changed & UseListViewForComboBoxItems) && inputMode() == TextInput)
d->chooseRightTextInputWidget();
if ((changed & UsePlainTextEditForTextInput) && inputMode() == TextInput)
d->chooseRightTextInputWidget();
}
QInputDialog::InputDialogOptions QInputDialog::options() const
@ -653,6 +691,8 @@ void QInputDialog::setTextValue(const QString &text)
setInputMode(TextInput);
if (d->inputWidget == d->lineEdit) {
d->lineEdit->setText(text);
} else if (d->inputWidget == d->plainTextEdit) {
d->plainTextEdit->setPlainText(text);
} else if (d->inputWidget == d->comboBox) {
d->setComboBoxText(text);
} else {
@ -1075,6 +1115,8 @@ void QInputDialog::setVisible(bool visible)
d->inputWidget->setFocus();
if (d->inputWidget == d->lineEdit) {
d->lineEdit->selectAll();
} else if (d->inputWidget == d->plainTextEdit) {
d->plainTextEdit->selectAll();
} else if (d->inputWidget == d->intSpinBox) {
d->intSpinBox->selectAll();
} else if (d->inputWidget == d->doubleSpinBox) {
@ -1144,7 +1186,7 @@ void QInputDialog::done(int result)
want to do this, you should create the dialog yourself using one of the
QInputDialog constructors.
\sa getInt(), getDouble(), getItem()
\sa getInt(), getDouble(), getItem(), getMultiLineText()
*/
QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label,
@ -1168,6 +1210,58 @@ QString QInputDialog::getText(QWidget *parent, const QString &title, const QStri
}
}
/*!
\since 5.2
Static convenience function to get a multiline string from the user.
\a title is the text which is displayed in the title bar of the dialog.
\a label is the text which is shown to the user (it should say what should
be entered).
\a text is the default text which is placed in the plain text edit.
\a inputMethodHints is the input method hints that will be used in the
edit widget if an input method is active.
If \a ok is nonnull \e *\a ok will be set to true if the user pressed
\uicontrol OK and to false if the user pressed \uicontrol Cancel. The dialog's parent
is \a parent. The dialog will be modal and uses the specified widget
\a flags.
If the dialog is accepted, this function returns the text in the dialog's
plain text edit. If the dialog is rejected, a null QString is returned.
Use this static function like this:
\snippet dialogs/standarddialogs/dialog.cpp 4
\warning Do not delete \a parent during the execution of the dialog. If you
want to do this, you should create the dialog yourself using one of the
QInputDialog constructors.
\sa getInt(), getDouble(), getItem(), getText()
*/
QString QInputDialog::getMultiLineText(QWidget *parent, const QString &title, const QString &label,
const QString &text, bool *ok, Qt::WindowFlags flags,
Qt::InputMethodHints inputMethodHints)
{
QInputDialog dialog(parent, flags);
dialog.setOptions(QInputDialog::UsePlainTextEditForTextInput);
dialog.setWindowTitle(title);
dialog.setLabelText(label);
dialog.setTextValue(text);
dialog.setInputMethodHints(inputMethodHints);
int ret = dialog.exec();
if (ok)
*ok = !!ret;
if (ret) {
return dialog.textValue();
} else {
return QString();
}
}
/*!
\since 4.5
@ -1196,7 +1290,7 @@ QString QInputDialog::getText(QWidget *parent, const QString &title, const QStri
want to do this, you should create the dialog yourself using one of the
QInputDialog constructors.
\sa getText(), getDouble(), getItem()
\sa getText(), getDouble(), getItem(), getMultiLineText()
*/
int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &label, int value,
@ -1248,7 +1342,7 @@ int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &l
want to do this, you should create the dialog yourself using one of the
QInputDialog constructors.
\sa getText(), getDouble(), getItem()
\sa getText(), getDouble(), getItem(), getMultiLineText()
*/
/*!
@ -1277,7 +1371,7 @@ int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &l
want to do this, you should create the dialog yourself using one of the
QInputDialog constructors.
\sa getText(), getInt(), getItem()
\sa getText(), getInt(), getItem(), getMultiLineText()
*/
double QInputDialog::getDouble(QWidget *parent, const QString &title, const QString &label,
@ -1331,7 +1425,7 @@ double QInputDialog::getDouble(QWidget *parent, const QString &title, const QStr
want to do this, you should create the dialog yourself using one of the
QInputDialog constructors.
\sa getText(), getInt(), getDouble()
\sa getText(), getInt(), getDouble(), getMultiLineText()
*/
QString QInputDialog::getItem(QWidget *parent, const QString &title, const QString &label,

View File

@ -78,8 +78,9 @@ class Q_WIDGETS_EXPORT QInputDialog : public QDialog
public:
enum InputDialogOption {
NoButtons = 0x00000001,
UseListViewForComboBoxItems = 0x00000002
NoButtons = 0x00000001,
UseListViewForComboBoxItems = 0x00000002,
UsePlainTextEditForTextInput = 0x00000004
};
Q_DECLARE_FLAGS(InputDialogOptions, InputDialogOption)
@ -168,6 +169,9 @@ public:
QLineEdit::EchoMode echo = QLineEdit::Normal,
const QString &text = QString(), bool *ok = 0, Qt::WindowFlags flags = 0,
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
static QString getMultiLineText(QWidget *parent, const QString &title, const QString &label,
const QString &text = QString(), bool *ok = 0, Qt::WindowFlags flags = 0,
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
static QString getItem(QWidget *parent, const QString &title, const QString &label,
const QStringList &items, int current = 0, bool editable = true,
bool *ok = 0, Qt::WindowFlags flags = 0,
@ -204,6 +208,7 @@ public:
private:
Q_DISABLE_COPY(QInputDialog)
Q_PRIVATE_SLOT(d_func(), void _q_textChanged(const QString&))
Q_PRIVATE_SLOT(d_func(), void _q_plainTextEditTextChanged())
Q_PRIVATE_SLOT(d_func(), void _q_currentRowChanged(const QModelIndex&, const QModelIndex&))
};