1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the dialog module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qinputdialog.h"
31
#ifndef QT_NO_INPUTDIALOG
35
#include "qlineedit.h"
36
#include "qpushbutton.h"
38
#include "qcombobox.h"
39
#include "qstackedlayout.h"
40
#include "qvalidator.h"
41
#include "qapplication.h"
43
#include "qdialog_p.h"
45
class QInputDialogPrivate : public QDialogPrivate
47
Q_DECLARE_PUBLIC(QInputDialog)
49
QInputDialogPrivate();
54
void init(const QString &label, QInputDialog::Type);
58
QInputDialogPrivate::QInputDialogPrivate()
64
void QInputDialogPrivate::init(const QString &lbl, QInputDialog::Type type)
67
QVBoxLayout *vbox = new QVBoxLayout(q);
71
label = new QLabel(lbl, q);
72
vbox->addWidget(label);
76
case QInputDialog::LineEdit:
77
input = new QLineEdit(q);
79
case QInputDialog::SpinBox:
80
input = new QSpinBox(q);
82
case QInputDialog::DoubleSpinBox:
83
input = new QDoubleSpinBox(q);
85
case QInputDialog::ComboBox:
86
case QInputDialog::EditableComboBox: {
87
QComboBox *combo = new QComboBox(q);
88
if (type == QInputDialog::EditableComboBox)
89
combo->setEditable(true);
94
vbox->addWidget(input);
97
QHBoxLayout *hbox = new QHBoxLayout;
99
vbox->addLayout(hbox, Qt::AlignRight);
101
ok = new QPushButton(QInputDialog::tr("OK"), q);
102
ok->setDefault(true);
103
QPushButton *cancel = new QPushButton(QInputDialog::tr("Cancel"), q);
105
QSize bs = ok->sizeHint().expandedTo(cancel->sizeHint());
106
ok->setFixedSize(bs);
107
cancel->setFixedSize(bs);
111
hbox->addWidget(cancel);
113
QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept()));
114
QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject()));
116
q->resize(q->sizeHint());
122
\brief The QInputDialog class provides a simple convenience dialog to get a single value from the user.
126
The input value can be a string, a number or an item from a list. A
127
label must be set to tell the user what they should enter.
129
Four static convenience functions are provided:
130
getText(), getInteger(), getDouble() and getItem(). All the
131
functions can be used in a similar way, for example:
133
\quotefromfile dialogs/standarddialogs/dialog.cpp
134
\skipuntil Dialog::setText
138
The \c ok variable is set to true if the user clicks \gui OK;
139
otherwise it is set to false.
141
\img inputdialogs.png Input Dialogs
143
The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
144
how to use QInputDialog as well as other built-in Qt dialogs.
150
\enum QInputDialog::Type
152
This enum specifies the type of the dialog, i.e. what kind of data you
153
want the user to input:
155
\value LineEdit A QLineEdit is used for obtaining string or numeric
156
input. The QLineEdit can be accessed using lineEdit().
158
\value SpinBox A QSpinBox is used for obtaining integer input.
159
Use spinBox() to access the QSpinBox.
161
\value ComboBox A read-only QComboBox is used to provide a fixed
162
list of choices from which the user can choose.
163
Use comboBox() to access the QComboBox.
165
\value EditableComboBox An editable QComboBox is used to provide a fixed
166
list of choices from which the user can choose, but which also
167
allows the user to enter their own value instead.
168
Use editableComboBox() to access the QComboBox.
173
Constructs the dialog. The \a label is the text which is shown to
174
the user (it should tell the user what they are expected to enter).
175
The \a parent is the dialog's parent widget. The \a type parameter
176
is used to specify which type of dialog to construct. The \a f
177
parameter is passed on to the QDialog constructor.
179
\sa getText(), getInteger(), getDouble(), getItem()
182
QInputDialog::QInputDialog(const QString &label, QWidget* parent, Type type, Qt::WFlags f)
183
: QDialog(*new QInputDialogPrivate, parent, f)
186
d->init(label, type);
190
Destroys the input dialog.
193
QInputDialog::~QInputDialog()
198
Static convenience function to get a string from the user. \a
199
title is the text which is displayed in the title bar of the
200
dialog. \a label is the text which is shown to the user (it should
201
say what should be entered). \a text is the default text which is
202
placed in the line edit. The \a mode is the echo mode the line
203
edit will use. If \a ok is non-null \e *\a ok will be set to true
204
if the user pressed \gui OK and to false if the user pressed
205
\gui Cancel. The dialog's parent is \a parent. The dialog will be
206
modal and uses the widget flags \a f.
208
This function returns the text which has been entered in the line
209
edit. It will not return an empty string.
211
Use this static function like this:
213
\quotefromfile dialogs/standarddialogs/dialog.cpp
214
\skipuntil Dialog::setText
218
\sa getInteger(), getDouble(), getItem()
221
QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label,
222
QLineEdit::EchoMode mode, const QString &text,
223
bool *ok, Qt::WFlags f)
225
QInputDialog dlg(label, parent, LineEdit, f);
226
dlg.setObjectName("qt_inputdlg_gettext");
228
#ifndef QT_NO_WIDGET_TOPEXTRA
229
dlg.setWindowTitle(title);
231
QLineEdit *le = qobject_cast<QLineEdit *>(dlg.d_func()->input);
233
le->setEchoMode(mode);
236
bool accepted = (dlg.exec() == QDialog::Accepted);
246
Static convenience function to get an integer input from the
247
user. \a title is the text which is displayed in the title bar
248
of the dialog. \a label is the text which is shown to the user
249
(it should say what should be entered). \a value is the default
250
integer which the spinbox will be set to. \a minValue and \a
251
maxValue are the minimum and maximum values the user may choose,
252
and \a step is the amount by which the values change as the user
253
presses the arrow buttons to increment or decrement the value.
255
If \a ok is non-null *\a ok will be set to true if the user
256
pressed \gui OK and to false if the user pressed \gui Cancel. The
257
dialog's parent is \a parent. The dialog will be modal and uses
258
the widget flags \a f.
260
This function returns the integer which has been entered by the user.
262
Use this static function like this:
264
\quotefromfile dialogs/standarddialogs/dialog.cpp
265
\skipuntil Dialog::setInteger
269
\sa getText(), getDouble(), getItem()
272
int QInputDialog::getInteger(QWidget *parent, const QString &title, const QString &label,
273
int value, int minValue, int maxValue, int step, bool *ok,
276
QInputDialog dlg(label, parent, SpinBox, f);
277
dlg.setObjectName("qt_inputdlg_getint");
279
#ifndef QT_NO_WIDGET_TOPEXTRA
280
dlg.setWindowTitle(title);
282
QSpinBox *sb = qobject_cast<QSpinBox *>(dlg.d_func()->input);
283
sb->setRange(minValue, maxValue);
284
sb->setSingleStep(step);
287
bool accepted = (dlg.exec() == QDialog::Accepted);
294
Static convenience function to get a floating point number from
295
the user. \a title is the text which is displayed in the title
296
bar of the dialog. \a label is the text which is shown to the user
297
(it should say what should be entered). \a value is the default
298
floating point number that the line edit will be set to. \a
299
minValue and \a maxValue are the minimum and maximum values the
300
user may choose, and \a decimals is the maximum number of decimal
301
places the number may have.
303
If \a ok is non-null, *\a ok will be set to true if the user
304
pressed OK and to false if the user pressed \gui Cancel. The
305
dialog's parent is \a parent. The dialog will be modal and uses
306
the widget flags \a f.
308
This function returns the floating point number which has been
311
Use this static function like this:
313
\quotefromfile dialogs/standarddialogs/dialog.cpp
314
\skipuntil Dialog::setDouble
318
\sa getText(), getInteger(), getItem()
321
double QInputDialog::getDouble( QWidget *parent, const QString &title, const QString &label,
322
double value, double minValue, double maxValue,
323
int decimals, bool *ok, Qt::WFlags f)
325
QInputDialog dlg(label, parent, DoubleSpinBox, f);
326
dlg.setObjectName("qt_inputdlg_getdbl");
327
#ifndef QT_NO_WIDGET_TOPEXTRA
328
dlg.setWindowTitle(title);
330
QDoubleSpinBox *sb = qobject_cast<QDoubleSpinBox *>(dlg.d_func()->input);
331
sb->setRange(minValue, maxValue);
332
sb->setDecimals(decimals);
335
bool accepted = (dlg.exec() == QDialog::Accepted);
342
Static convenience function to let the user select an item from a
343
string list. \a title is the text which is displayed in the title
344
bar of the dialog. \a label is the text which is shown to the user (it
345
should say what should be entered). \a list is the
346
string list which is inserted into the combobox, and \a current is the number
347
of the item which should be the current item. If \a editable is true
348
the user can enter their own text; if \a editable is false the user
349
may only select one of the existing items.
351
If \a ok is non-null \e *\a ok will be set to true if the user
352
pressed OK and to false if the user pressed \gui Cancel. The
353
dialog's parent is \a parent. The dialog will be modal and uses
354
the widget flags \a f.
356
This function returns the text of the current item, or if \a
357
editable is true, the current text of the combobox.
359
Use this static function like this:
361
\quotefromfile dialogs/standarddialogs/dialog.cpp
362
\skipuntil Dialog::setItem
366
\sa getText(), getInteger(), getDouble()
369
QString QInputDialog::getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &list,
370
int current, bool editable, bool *ok, Qt::WFlags f)
372
QInputDialog dlg(label, parent, editable ? EditableComboBox : ComboBox, f);
373
dlg.setObjectName("qt_inputdlg_getitem");
374
#ifndef QT_NO_WIDGET_TOPEXTRA
375
dlg.setWindowTitle(title);
378
QComboBox *combo = qobject_cast<QComboBox *>(dlg.d_func()->input);
379
combo->addItems(list);
380
combo->setCurrentIndex(current);
382
bool accepted = (dlg.exec() == QDialog::Accepted);
385
return combo->currentText();
389
\fn QString QInputDialog::getText(const QString &title, const QString &label,
390
QLineEdit::EchoMode echo = QLineEdit::Normal,
391
const QString &text = QString(), bool *ok = 0,
392
QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
394
Call getText(\a parent, \a title, \a label, \a echo, \a text, \a
397
The \a name parameter is ignored.
401
\fn int QInputDialog::getInteger(const QString &title, const QString &label, int value = 0,
402
int minValue = -2147483647, int maxValue = 2147483647,
403
int step = 1, bool *ok = 0,
404
QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
407
Call getInteger(\a parent, \a title, \a label, \a value, \a
408
minValue, \a maxValue, \a step, \a ok, \a f) instead.
410
The \a name parameter is ignored.
414
\fn double QInputDialog::getDouble(const QString &title, const QString &label, double value = 0,
415
double minValue = -2147483647, double maxValue = 2147483647,
416
int decimals = 1, bool *ok = 0,
417
QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
419
Call getDouble(\a parent, \a title, \a label, \a value, \a
420
minValue, \a maxValue, \a decimals, \a ok, \a f).
422
The \a name parameter is ignored.
426
\fn QString QInputDialog::getItem(const QString &title, const QString &label, const QStringList &list,
427
int current = 0, bool editable = true, bool *ok = 0,
428
QWidget *parent = 0, const char *name = 0, Qt::WFlags f = 0)
430
Call getItem(\a parent, \a title, \a label, \a list, \a current,
431
\a editable, \a ok, \a f) instead.
433
The \a name parameter is ignored.