Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qinputdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qinputdialog.h"
6
7#include "qapplication.h"
8#include "qcombobox.h"
10#include "qlabel.h"
11#include "qlayout.h"
12#include "qlineedit.h"
13#include "qplaintextedit.h"
14#include "qlistview.h"
15#include "qpushbutton.h"
16#include "qspinbox.h"
17#include "qstackedlayout.h"
18#include "qvalidator.h"
19#include "qevent.h"
20#include "qdialog_p.h"
21
22#include <QtCore/qpointer.h>
23
24QT_USE_NAMESPACE
25
33
34static const char *candidateSignal(int which)
35{
36 switch (CandidateSignal(which)) {
37 case TextValueSelectedSignal: return SIGNAL(textValueSelected(QString));
38 case IntValueSelectedSignal: return SIGNAL(intValueSelected(int));
39 case DoubleValueSelectedSignal: return SIGNAL(doubleValueSelected(double));
40
42 break;
43 };
44 Q_UNREACHABLE_RETURN(nullptr);
45}
46
47static const char *signalForMember(const char *member)
48{
49 QByteArray normalizedMember(QMetaObject::normalizedSignature(member));
50
51 for (int i = 0; i < NumCandidateSignals; ++i)
52 if (QMetaObject::checkConnectArgs(candidateSignal(i), normalizedMember))
53 return candidateSignal(i);
54
55 // otherwise, use fit-all accepted signal:
56 return SIGNAL(accepted());
57}
58
59QT_BEGIN_NAMESPACE
60
61/*
62 These internal classes add extra validation to QSpinBox and QDoubleSpinBox by emitting
63 textChanged(bool) after events that may potentially change the visible text. Return or
64 Enter key presses are not propagated if the visible text is invalid. Instead, the visible
65 text is modified to the last valid value.
66*/
67class QInputDialogSpinBox : public QSpinBox
68{
69 Q_OBJECT
70
71public:
72 QInputDialogSpinBox(QWidget *parent)
73 : QSpinBox(parent) {
74 connect(lineEdit(), &QLineEdit::textChanged,
75 this, &QInputDialogSpinBox::notifyTextChanged);
76 connect(this, &QInputDialogSpinBox::editingFinished,
77 this, &QInputDialogSpinBox::notifyTextChanged);
78 }
79
80signals:
81 void textChanged(bool);
82
83private slots:
84 void notifyTextChanged() { emit textChanged(hasAcceptableInput()); }
85
86private:
87 void keyPressEvent(QKeyEvent *event) override {
88 if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !hasAcceptableInput()) {
89 setProperty("value", property("value"));
90 } else {
91 QSpinBox::keyPressEvent(event);
92 }
93 notifyTextChanged();
94 }
95
96 void mousePressEvent(QMouseEvent *event) override {
97 QSpinBox::mousePressEvent(event);
98 notifyTextChanged();
99 }
100};
101
136
138{
139public:
140 QInputDialogListView(QWidget *parent = nullptr) : QListView(parent) {}
141 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override
142 {
143 if (query == Qt::ImEnabled)
144 return false;
145 return QListView::inputMethodQuery(query);
146 }
147};
148
150{
151 Q_DECLARE_PUBLIC(QInputDialog)
152
153public:
155
163 void ensureEnabledConnection(QAbstractSpinBox *spinBox);
164 void setInputWidget(QWidget *widget);
166 void setComboBoxText(const QString &text);
167 void setListViewText(const QString &text);
169 void ensureLayout() const { const_cast<QInputDialogPrivate *>(this)->ensureLayout(); }
170 bool useComboBoxOrListView() const { return comboBox && comboBox->count() > 0; }
171 void textChanged(const QString &text);
173 void currentRowChanged(const QModelIndex &newIndex, const QModelIndex &oldIndex);
174
175 mutable QLabel *label;
177 mutable QLineEdit *lineEdit;
189};
190
191QInputDialogPrivate::QInputDialogPrivate()
192 : label(nullptr), buttonBox(nullptr), lineEdit(nullptr), plainTextEdit(nullptr), intSpinBox(nullptr), doubleSpinBox(nullptr),
193 comboBox(nullptr), listView(nullptr), inputWidget(nullptr), mainLayout(nullptr)
194{
195}
196
198{
199 Q_Q(QInputDialog);
200
201 if (mainLayout)
202 return;
203
204 if (!inputWidget) {
206 inputWidget = lineEdit;
207 }
208
209 if (!label)
210 label = new QLabel(QInputDialog::tr("Enter a value:"), q);
211#ifndef QT_NO_SHORTCUT
212 label->setBuddy(inputWidget);
213#endif
214 label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
215
216 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, q);
217 QObject::connect(buttonBox, &QDialogButtonBox::accepted, q, &QDialog::accept);
218 QObject::connect(buttonBox, &QDialogButtonBox::rejected, q, &QDialog::reject);
219
220 mainLayout = new QVBoxLayout(q);
221 mainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
222 mainLayout->addWidget(label);
223 mainLayout->addWidget(inputWidget);
224 mainLayout->addWidget(buttonBox);
225 ensureEnabledConnection(qobject_cast<QAbstractSpinBox *>(inputWidget));
226 inputWidget->show();
227}
228
230{
231 Q_Q(QInputDialog);
232 if (!lineEdit) {
233 lineEdit = new QLineEdit(q);
234#ifndef QT_NO_IM
235 qt_widget_private(lineEdit)->inheritsInputMethodHints = 1;
236#endif
237 lineEdit->hide();
238 QObjectPrivate::connect(lineEdit, &QLineEdit::textChanged,
239 this, &QInputDialogPrivate::textChanged);
240 }
241}
242
244{
245 Q_Q(QInputDialog);
246 if (!plainTextEdit) {
247 plainTextEdit = new QPlainTextEdit(q);
248 plainTextEdit->setLineWrapMode(QPlainTextEdit::NoWrap);
249#ifndef QT_NO_IM
250 qt_widget_private(plainTextEdit)->inheritsInputMethodHints = 1;
251#endif
252 plainTextEdit->hide();
253 QObjectPrivate::connect(plainTextEdit, &QPlainTextEdit::textChanged,
254 this, &QInputDialogPrivate::plainTextEditTextChanged);
255 }
256}
257
259{
260 Q_Q(QInputDialog);
261 if (!comboBox) {
262 comboBox = new QComboBox(q);
263#ifndef QT_NO_IM
264 qt_widget_private(comboBox)->inheritsInputMethodHints = 1;
265#endif
266 comboBox->hide();
267 QObjectPrivate::connect(comboBox, &QComboBox::editTextChanged,
268 this, &QInputDialogPrivate::textChanged);
269 QObjectPrivate::connect(comboBox, &QComboBox::currentTextChanged,
270 this, &QInputDialogPrivate::textChanged);
271 }
272}
273
275{
276 Q_Q(QInputDialog);
277 if (!listView) {
279 listView = new QInputDialogListView(q);
280 listView->hide();
281 listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
282 listView->setSelectionMode(QAbstractItemView::SingleSelection);
283 listView->setModel(comboBox->model());
284 listView->setCurrentIndex(QModelIndex()); // ###
285 QObjectPrivate::connect(listView->selectionModel(),
286 &QItemSelectionModel::currentRowChanged,
287 this, &QInputDialogPrivate::currentRowChanged);
288 }
289}
290
292{
293 Q_Q(QInputDialog);
294 if (!intSpinBox) {
295 intSpinBox = new QInputDialogSpinBox(q);
296 intSpinBox->hide();
297 QObject::connect(intSpinBox, &QInputDialogSpinBox::valueChanged,
298 q, &QInputDialog::intValueChanged);
299 }
300}
301
303{
304 Q_Q(QInputDialog);
305 if (!doubleSpinBox) {
306 doubleSpinBox = new QInputDialogDoubleSpinBox(q);
307 doubleSpinBox->hide();
308 QObject::connect(doubleSpinBox, &QInputDialogDoubleSpinBox::valueChanged,
309 q, &QInputDialog::doubleValueChanged);
310 }
311}
312
313void QInputDialogPrivate::ensureEnabledConnection(QAbstractSpinBox *spinBox)
314{
315 if (spinBox) {
316 QAbstractButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
317 QObject::connect(spinBox, SIGNAL(textChanged(bool)), okButton, SLOT(setEnabled(bool)), Qt::UniqueConnection);
318 }
319}
320
322{
323 Q_ASSERT(widget);
324 if (inputWidget == widget)
325 return;
326
327 if (mainLayout) {
328 Q_ASSERT(inputWidget);
329 mainLayout->removeWidget(inputWidget);
330 inputWidget->hide();
331 mainLayout->insertWidget(1, widget);
332 widget->show();
333
334 // disconnect old input widget
335 QAbstractButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
336 if (QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(inputWidget))
337 QObject::disconnect(spinBox, SIGNAL(textChanged(bool)), okButton, SLOT(setEnabled(bool)));
338
339 // connect new input widget and update enabled state of OK button
340 QAbstractSpinBox *spinBox = qobject_cast<QAbstractSpinBox *>(widget);
342 okButton->setEnabled(!spinBox || spinBox->hasAcceptableInput());
343 }
344
345 inputWidget = widget;
346
347 // synchronize the text shown in the new text editor with the current
348 // textValue
349 if (widget == lineEdit) {
350 lineEdit->setText(textValue);
351 } else if (widget == plainTextEdit) {
352 plainTextEdit->setPlainText(textValue);
353 } else if (widget == comboBox) {
354 setComboBoxText(textValue);
355 } else if (widget == listView) {
356 setListViewText(textValue);
358 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(listView->selectionModel()->hasSelection());
359 }
360}
361
363{
364 QWidget *widget;
365
367 if ((opts & QInputDialog::UseListViewForComboBoxItems) && !comboBox->isEditable()) {
369 widget = listView;
370 } else {
371 widget = comboBox;
372 }
373 } else if (opts & QInputDialog::UsePlainTextEditForTextInput) {
375 widget = plainTextEdit;
376 } else {
378 widget = lineEdit;
379 }
380
381 setInputWidget(widget);
382
383 if (inputWidget == comboBox) {
384 textChanged(comboBox->currentText());
385 } else if (inputWidget == listView) {
386 textChanged(listViewText());
387 }
388}
389
390void QInputDialogPrivate::setComboBoxText(const QString &text)
391{
392 int index = comboBox->findText(text);
393 if (index != -1) {
394 comboBox->setCurrentIndex(index);
395 } else if (comboBox->isEditable()) {
396 comboBox->setEditText(text);
397 }
398}
399
400void QInputDialogPrivate::setListViewText(const QString &text)
401{
402 int row = comboBox->findText(text);
403 if (row != -1) {
404 QModelIndex index(comboBox->model()->index(row, 0));
405 listView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Clear
406 | QItemSelectionModel::SelectCurrent);
407 }
408}
409
411{
412 if (listView->selectionModel()->hasSelection()) {
413 int row = listView->selectionModel()->selectedRows().value(0).row();
414 return comboBox->itemText(row);
415 } else {
416 return QString();
417 }
418}
419
420void QInputDialogPrivate::textChanged(const QString &text)
421{
422 Q_Q(QInputDialog);
423 if (textValue != text) {
424 textValue = text;
425 emit q->textValueChanged(text);
426 }
427}
428
430{
431 Q_Q(QInputDialog);
432 QString text = plainTextEdit->toPlainText();
433 if (textValue != text) {
434 textValue = text;
435 emit q->textValueChanged(text);
436 }
437}
438
439void QInputDialogPrivate::currentRowChanged(const QModelIndex &newIndex,
440 const QModelIndex & /* oldIndex */)
441{
442 textChanged(comboBox->model()->data(newIndex).toString());
443 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
444}
445
446/*!
447 \class QInputDialog
448 \brief The QInputDialog class provides a simple convenience dialog to get a
449 single value from the user.
450 \ingroup standard-dialogs
451 \inmodule QtWidgets
452
453 The input value can be a string, a number or an item from a list. A label
454 must be set to tell the user what they should enter.
455
456 Five static convenience functions are provided: getText(), getMultiLineText(),
457 getInt(), getDouble(), and getItem(). All the functions can be used in a similar way,
458 for example:
459
460 \snippet dialogs/standarddialogs/dialog.cpp 3
461
462 The \c ok variable is set to true if the user clicks \uicontrol OK; otherwise, it
463 is set to false.
464
465 \image inputdialogs.png Input Dialogs
466
467 The \l{dialogs/standarddialogs}{Standard Dialogs} example shows how to use
468 QInputDialog as well as other built-in Qt dialogs.
469
470 \sa QMessageBox, {Standard Dialogs Example}
471*/
472
473/*!
474 \enum QInputDialog::InputMode
475
476 This enum describes the different modes of input that can be selected for
477 the dialog.
478
479 \value TextInput Used to input text strings.
480 \value IntInput Used to input integers.
481 \value DoubleInput Used to input floating point numbers with double
482 precision accuracy.
483
484 \sa inputMode
485*/
486
487/*!
488 Constructs a new input dialog with the given \a parent and window \a flags.
489*/
490QInputDialog::QInputDialog(QWidget *parent, Qt::WindowFlags flags)
491 : QDialog(*new QInputDialogPrivate, parent, flags)
492{
493}
494
495/*!
496 Destroys the input dialog.
497*/
498QInputDialog::~QInputDialog()
499{
500}
501
502/*!
503 \property QInputDialog::inputMode
504
505 \brief the mode used for input
506
507 This property helps determine which widget is used for entering input into
508 the dialog.
509*/
510void QInputDialog::setInputMode(InputMode mode)
511{
512 Q_D(QInputDialog);
513
514 QWidget *widget;
515
516 /*
517 Warning: Some functions in QInputDialog rely on implementation details
518 of the code below. Look for the comments that accompany the calls to
519 setInputMode() throughout this file before you change the code below.
520 */
521
522 switch (mode) {
523 case IntInput:
524 d->ensureIntSpinBox();
525 widget = d->intSpinBox;
526 break;
527 case DoubleInput:
528 d->ensureDoubleSpinBox();
529 widget = d->doubleSpinBox;
530 break;
531 default:
532 Q_ASSERT(mode == TextInput);
533 d->chooseRightTextInputWidget();
534 return;
535 }
536
537 d->setInputWidget(widget);
538}
539
540QInputDialog::InputMode QInputDialog::inputMode() const
541{
542 Q_D(const QInputDialog);
543
544 if (d->inputWidget) {
545 if (d->inputWidget == d->intSpinBox) {
546 return IntInput;
547 } else if (d->inputWidget == d->doubleSpinBox) {
548 return DoubleInput;
549 }
550 }
551
552 return TextInput;
553}
554
555/*!
556 \property QInputDialog::labelText
557
558 \brief the label's text which describes what needs to be input
559*/
560void QInputDialog::setLabelText(const QString &text)
561{
562 Q_D(QInputDialog);
563 if (!d->label) {
564 d->label = new QLabel(text, this);
565 } else {
566 d->label->setText(text);
567 }
568}
569
570QString QInputDialog::labelText() const
571{
572 Q_D(const QInputDialog);
573 d->ensureLayout();
574 return d->label->text();
575}
576
577/*!
578 \enum QInputDialog::InputDialogOption
579
580 This enum specifies various options that affect the look and feel
581 of an input dialog.
582
583 \value NoButtons Don't display \uicontrol{OK} and \uicontrol{Cancel} buttons (useful for "live dialogs").
584 \value UseListViewForComboBoxItems Use a QListView rather than a non-editable QComboBox for
585 displaying the items set with setComboBoxItems().
586 \value UsePlainTextEditForTextInput Use a QPlainTextEdit for multiline text input. This value was
587 introduced in 5.2.
588
589 \sa options, setOption(), testOption()
590*/
591
592/*!
593 Sets the given \a option to be enabled if \a on is true;
594 otherwise, clears the given \a option.
595
596 \sa options, testOption()
597*/
598void QInputDialog::setOption(InputDialogOption option, bool on)
599{
600 Q_D(QInputDialog);
601 if (!(d->opts & option) != !on)
602 setOptions(d->opts ^ option);
603}
604
605/*!
606 Returns \c true if the given \a option is enabled; otherwise, returns
607 false.
608
609 \sa options, setOption()
610*/
611bool QInputDialog::testOption(InputDialogOption option) const
612{
613 Q_D(const QInputDialog);
614 return (d->opts & option) != 0;
615}
616
617/*!
618 \property QInputDialog::options
619 \brief the various options that affect the look and feel of the dialog
620
621 By default, all options are disabled.
622
623 \sa setOption(), testOption()
624*/
625void QInputDialog::setOptions(InputDialogOptions options)
626{
627 Q_D(QInputDialog);
628
629 InputDialogOptions changed = (options ^ d->opts);
630 if (!changed)
631 return;
632
633 d->opts = options;
634 d->ensureLayout();
635
636 if (changed & NoButtons)
637 d->buttonBox->setVisible(!(options & NoButtons));
638 if ((changed & UseListViewForComboBoxItems) && inputMode() == TextInput)
639 d->chooseRightTextInputWidget();
640 if ((changed & UsePlainTextEditForTextInput) && inputMode() == TextInput)
641 d->chooseRightTextInputWidget();
642}
643
644QInputDialog::InputDialogOptions QInputDialog::options() const
645{
646 Q_D(const QInputDialog);
647 return d->opts;
648}
649
650/*!
651 \property QInputDialog::textValue
652
653 \brief the text value for the input dialog
654
655 This property is only relevant when the input dialog is used in
656 TextInput mode.
657*/
658void QInputDialog::setTextValue(const QString &text)
659{
660 Q_D(QInputDialog);
661
662 setInputMode(TextInput);
663 if (d->inputWidget == d->lineEdit) {
664 d->lineEdit->setText(text);
665 } else if (d->inputWidget == d->plainTextEdit) {
666 d->plainTextEdit->setPlainText(text);
667 } else if (d->inputWidget == d->comboBox) {
668 d->setComboBoxText(text);
669 } else {
670 d->setListViewText(text);
671 }
672}
673
674QString QInputDialog::textValue() const
675{
676 Q_D(const QInputDialog);
677 return d->textValue;
678}
679
680/*!
681 \property QInputDialog::textEchoMode
682
683 \brief the echo mode for the text value
684
685 This property is only relevant when the input dialog is used in
686 TextInput mode.
687*/
688void QInputDialog::setTextEchoMode(QLineEdit::EchoMode mode)
689{
690 Q_D(QInputDialog);
691 d->ensureLineEdit();
692 d->lineEdit->setEchoMode(mode);
693}
694
695QLineEdit::EchoMode QInputDialog::textEchoMode() const
696{
697 Q_D(const QInputDialog);
698 if (d->lineEdit) {
699 return d->lineEdit->echoMode();
700 } else {
701 return QLineEdit::Normal;
702 }
703}
704
705/*!
706 \property QInputDialog::comboBoxEditable
707
708 \brief whether or not the combo box used in the input dialog is editable
709*/
710void QInputDialog::setComboBoxEditable(bool editable)
711{
712 Q_D(QInputDialog);
713 d->ensureComboBox();
714 d->comboBox->setEditable(editable);
715 if (inputMode() == TextInput)
716 d->chooseRightTextInputWidget();
717}
718
719bool QInputDialog::isComboBoxEditable() const
720{
721 Q_D(const QInputDialog);
722 if (d->comboBox) {
723 return d->comboBox->isEditable();
724 } else {
725 return false;
726 }
727}
728
729/*!
730 \property QInputDialog::comboBoxItems
731
732 \brief the items used in the combo box for the input dialog
733*/
734void QInputDialog::setComboBoxItems(const QStringList &items)
735{
736 Q_D(QInputDialog);
737
738 d->ensureComboBox();
739 {
740 const QSignalBlocker blocker(d->comboBox);
741 d->comboBox->clear();
742 d->comboBox->addItems(items);
743 }
744
745 if (inputMode() == TextInput)
746 d->chooseRightTextInputWidget();
747}
748
749QStringList QInputDialog::comboBoxItems() const
750{
751 Q_D(const QInputDialog);
752 QStringList result;
753 if (d->comboBox) {
754 const int count = d->comboBox->count();
755 result.reserve(count);
756 for (int i = 0; i < count; ++i)
757 result.append(d->comboBox->itemText(i));
758 }
759 return result;
760}
761
762/*!
763 \property QInputDialog::intValue
764 \brief the current integer value accepted as input
765
766 This property is only relevant when the input dialog is used in
767 IntInput mode.
768*/
769void QInputDialog::setIntValue(int value)
770{
771 Q_D(QInputDialog);
772 setInputMode(IntInput);
773 d->intSpinBox->setValue(value);
774}
775
776int QInputDialog::intValue() const
777{
778 Q_D(const QInputDialog);
779 if (d->intSpinBox) {
780 return d->intSpinBox->value();
781 } else {
782 return 0;
783 }
784}
785
786/*!
787 \property QInputDialog::intMinimum
788 \brief the minimum integer value accepted as input
789
790 This property is only relevant when the input dialog is used in
791 IntInput mode.
792*/
793void QInputDialog::setIntMinimum(int min)
794{
795 Q_D(QInputDialog);
796 d->ensureIntSpinBox();
797 d->intSpinBox->setMinimum(min);
798}
799
800int QInputDialog::intMinimum() const
801{
802 Q_D(const QInputDialog);
803 if (d->intSpinBox) {
804 return d->intSpinBox->minimum();
805 } else {
806 return 0;
807 }
808}
809
810/*!
811 \property QInputDialog::intMaximum
812 \brief the maximum integer value accepted as input
813
814 This property is only relevant when the input dialog is used in
815 IntInput mode.
816*/
817void QInputDialog::setIntMaximum(int max)
818{
819 Q_D(QInputDialog);
820 d->ensureIntSpinBox();
821 d->intSpinBox->setMaximum(max);
822}
823
824int QInputDialog::intMaximum() const
825{
826 Q_D(const QInputDialog);
827 if (d->intSpinBox) {
828 return d->intSpinBox->maximum();
829 } else {
830 return 99;
831 }
832}
833
834/*!
835 Sets the range of integer values accepted by the dialog when used in
836 IntInput mode, with minimum and maximum values specified by \a min and
837 \a max respectively.
838*/
839void QInputDialog::setIntRange(int min, int max)
840{
841 Q_D(QInputDialog);
842 d->ensureIntSpinBox();
843 d->intSpinBox->setRange(min, max);
844}
845
846/*!
847 \property QInputDialog::intStep
848 \brief the step by which the integer value is increased and decreased
849
850 This property is only relevant when the input dialog is used in
851 IntInput mode.
852*/
853void QInputDialog::setIntStep(int step)
854{
855 Q_D(QInputDialog);
856 d->ensureIntSpinBox();
857 d->intSpinBox->setSingleStep(step);
858}
859
860int QInputDialog::intStep() const
861{
862 Q_D(const QInputDialog);
863 if (d->intSpinBox) {
864 return d->intSpinBox->singleStep();
865 } else {
866 return 1;
867 }
868}
869
870/*!
871 \property QInputDialog::doubleValue
872 \brief the current double precision floating point value accepted as input
873
874 This property is only relevant when the input dialog is used in
875 DoubleInput mode.
876*/
877void QInputDialog::setDoubleValue(double value)
878{
879 Q_D(QInputDialog);
880 setInputMode(DoubleInput);
881 d->doubleSpinBox->setValue(value);
882}
883
884double QInputDialog::doubleValue() const
885{
886 Q_D(const QInputDialog);
887 if (d->doubleSpinBox) {
888 return d->doubleSpinBox->value();
889 } else {
890 return 0.0;
891 }
892}
893
894/*!
895 \property QInputDialog::doubleMinimum
896 \brief the minimum double precision floating point value accepted as input
897
898 This property is only relevant when the input dialog is used in
899 DoubleInput mode.
900*/
901void QInputDialog::setDoubleMinimum(double min)
902{
903 Q_D(QInputDialog);
904 d->ensureDoubleSpinBox();
905 d->doubleSpinBox->setMinimum(min);
906}
907
908double QInputDialog::doubleMinimum() const
909{
910 Q_D(const QInputDialog);
911 if (d->doubleSpinBox) {
912 return d->doubleSpinBox->minimum();
913 } else {
914 return 0.0;
915 }
916}
917
918/*!
919 \property QInputDialog::doubleMaximum
920 \brief the maximum double precision floating point value accepted as input
921
922 This property is only relevant when the input dialog is used in
923 DoubleInput mode.
924*/
925void QInputDialog::setDoubleMaximum(double max)
926{
927 Q_D(QInputDialog);
928 d->ensureDoubleSpinBox();
929 d->doubleSpinBox->setMaximum(max);
930}
931
932double QInputDialog::doubleMaximum() const
933{
934 Q_D(const QInputDialog);
935 if (d->doubleSpinBox) {
936 return d->doubleSpinBox->maximum();
937 } else {
938 return 99.99;
939 }
940}
941
942/*!
943 Sets the range of double precision floating point values accepted by the
944 dialog when used in DoubleInput mode, with minimum and maximum values
945 specified by \a min and \a max respectively.
946*/
947void QInputDialog::setDoubleRange(double min, double max)
948{
949 Q_D(QInputDialog);
950 d->ensureDoubleSpinBox();
951 d->doubleSpinBox->setRange(min, max);
952}
953
954/*!
955 \property QInputDialog::doubleDecimals
956
957 \brief sets the precision of the double spinbox in decimals
958
959 \sa QDoubleSpinBox::setDecimals()
960*/
961void QInputDialog::setDoubleDecimals(int decimals)
962{
963 Q_D(QInputDialog);
964 d->ensureDoubleSpinBox();
965 d->doubleSpinBox->setDecimals(decimals);
966}
967
968int QInputDialog::doubleDecimals() const
969{
970 Q_D(const QInputDialog);
971 if (d->doubleSpinBox) {
972 return d->doubleSpinBox->decimals();
973 } else {
974 return 2;
975 }
976}
977
978/*!
979 \property QInputDialog::okButtonText
980
981 \brief the text for the button used to accept the entry in the dialog
982*/
983void QInputDialog::setOkButtonText(const QString &text)
984{
985 Q_D(const QInputDialog);
986 d->ensureLayout();
987 d->buttonBox->button(QDialogButtonBox::Ok)->setText(text);
988}
989
990QString QInputDialog::okButtonText() const
991{
992 Q_D(const QInputDialog);
993 d->ensureLayout();
994 return d->buttonBox->button(QDialogButtonBox::Ok)->text();
995}
996
997/*!
998 \property QInputDialog::cancelButtonText
999 \brief the text for the button used to cancel the dialog
1000*/
1001void QInputDialog::setCancelButtonText(const QString &text)
1002{
1003 Q_D(const QInputDialog);
1004 d->ensureLayout();
1005 d->buttonBox->button(QDialogButtonBox::Cancel)->setText(text);
1006}
1007
1008QString QInputDialog::cancelButtonText() const
1009{
1010 Q_D(const QInputDialog);
1011 d->ensureLayout();
1012 return d->buttonBox->button(QDialogButtonBox::Cancel)->text();
1013}
1014
1015/*!
1016 This function connects one of its signals to the slot specified by \a receiver
1017 and \a member. The specific signal depends on the arguments that are specified
1018 in \a member. These are:
1019
1020 \list
1021 \li textValueSelected() if \a member has a QString for its first argument.
1022 \li intValueSelected() if \a member has an int for its first argument.
1023 \li doubleValueSelected() if \a member has a double for its first argument.
1024 \li accepted() if \a member has NO arguments.
1025 \endlist
1026
1027 The signal will be disconnected from the slot when the dialog is closed.
1028*/
1029void QInputDialog::open(QObject *receiver, const char *member)
1030{
1031 Q_D(QInputDialog);
1032 connect(this, signalForMember(member), receiver, member);
1033 d->receiverToDisconnectOnClose = receiver;
1034 d->memberToDisconnectOnClose = member;
1035 QDialog::open();
1036}
1037
1038/*!
1039 \reimp
1040*/
1041QSize QInputDialog::minimumSizeHint() const
1042{
1043 Q_D(const QInputDialog);
1044 d->ensureLayout();
1045 return QDialog::minimumSizeHint();
1046}
1047
1048/*!
1049 \reimp
1050*/
1051QSize QInputDialog::sizeHint() const
1052{
1053 Q_D(const QInputDialog);
1054 d->ensureLayout();
1055 return QDialog::sizeHint();
1056}
1057
1058/*!
1059 \reimp
1060*/
1061void QInputDialog::setVisible(bool visible)
1062{
1063 Q_D(const QInputDialog);
1064 if (visible) {
1065 d->ensureLayout();
1066 d->inputWidget->setFocus();
1067 if (d->inputWidget == d->lineEdit) {
1068 d->lineEdit->selectAll();
1069 } else if (d->inputWidget == d->plainTextEdit) {
1070 d->plainTextEdit->selectAll();
1071 } else if (d->inputWidget == d->intSpinBox) {
1072 d->intSpinBox->selectAll();
1073 } else if (d->inputWidget == d->doubleSpinBox) {
1074 d->doubleSpinBox->selectAll();
1075 }
1076 }
1077 QDialog::setVisible(visible);
1078}
1079
1080/*!
1081 Closes the dialog and sets its result code to \a result. If this dialog
1082 is shown with exec(), done() causes the local event loop to finish,
1083 and exec() to return \a result.
1084
1085 \sa QDialog::done()
1086*/
1087void QInputDialog::done(int result)
1088{
1089 Q_D(QInputDialog);
1090 QDialog::done(result);
1091 if (result) {
1092 InputMode mode = inputMode();
1093 switch (mode) {
1094 case DoubleInput:
1095 emit doubleValueSelected(doubleValue());
1096 break;
1097 case IntInput:
1098 emit intValueSelected(intValue());
1099 break;
1100 default:
1101 Q_ASSERT(mode == TextInput);
1102 emit textValueSelected(textValue());
1103 }
1104 }
1105 if (d->receiverToDisconnectOnClose) {
1106 disconnect(this, signalForMember(d->memberToDisconnectOnClose),
1107 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
1108 d->receiverToDisconnectOnClose = nullptr;
1109 }
1110 d->memberToDisconnectOnClose.clear();
1111}
1112
1113/*!
1114 Static convenience function to get a string from the user.
1115
1116 \a title is the text which is displayed in the title bar of the dialog.
1117 \a label is the text which is shown to the user (it should say what should
1118 be entered).
1119 \a text is the default text which is placed in the line edit.
1120 \a mode is the echo mode the line edit will use.
1121 \a inputMethodHints is the input method hints that will be used in the
1122 edit widget if an input method is active.
1123
1124 If \a ok is nonnull \e {*ok} will be set to true if the user pressed
1125 \uicontrol OK and to false if the user pressed \uicontrol Cancel. The dialog's parent
1126 is \a parent. The dialog will be modal and uses the specified widget
1127 \a flags.
1128
1129 If the dialog is accepted, this function returns the text in the dialog's
1130 line edit. If the dialog is rejected, a null QString is returned.
1131
1132 Use this static function like this:
1133
1134 \snippet dialogs/standarddialogs/dialog.cpp 3
1135
1136 \sa getInt(), getDouble(), getItem(), getMultiLineText()
1137*/
1138
1139QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label,
1140 QLineEdit::EchoMode mode, const QString &text, bool *ok,
1141 Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
1142{
1143 QAutoPointer<QInputDialog> dialog(new QInputDialog(parent, flags));
1144 dialog->setWindowTitle(title);
1145 dialog->setLabelText(label);
1146 dialog->setTextValue(text);
1147 dialog->setTextEchoMode(mode);
1148 dialog->setInputMethodHints(inputMethodHints);
1149
1150 const int ret = dialog->exec();
1151 if (ok)
1152 *ok = !!ret;
1153 if (bool(dialog) && ret) {
1154 return dialog->textValue();
1155 } else {
1156 return QString();
1157 }
1158}
1159
1160/*!
1161 \since 5.2
1162
1163 Static convenience function to get a multiline string from the user.
1164
1165 \a title is the text which is displayed in the title bar of the dialog.
1166 \a label is the text which is shown to the user (it should say what should
1167 be entered).
1168 \a text is the default text which is placed in the plain text edit.
1169 \a inputMethodHints is the input method hints that will be used in the
1170 edit widget if an input method is active.
1171
1172 If \a ok is nonnull \e {*ok} will be set to true if the user pressed
1173 \uicontrol OK and to false if the user pressed \uicontrol Cancel. The dialog's parent
1174 is \a parent. The dialog will be modal and uses the specified widget
1175 \a flags.
1176
1177 If the dialog is accepted, this function returns the text in the dialog's
1178 plain text edit. If the dialog is rejected, a null QString is returned.
1179
1180 Use this static function like this:
1181
1182 \snippet dialogs/standarddialogs/dialog.cpp 4
1183
1184 \sa getInt(), getDouble(), getItem(), getText()
1185*/
1186
1187QString QInputDialog::getMultiLineText(QWidget *parent, const QString &title, const QString &label,
1188 const QString &text, bool *ok, Qt::WindowFlags flags,
1189 Qt::InputMethodHints inputMethodHints)
1190{
1191 QAutoPointer<QInputDialog> dialog(new QInputDialog(parent, flags));
1192 dialog->setOptions(QInputDialog::UsePlainTextEditForTextInput);
1193 dialog->setWindowTitle(title);
1194 dialog->setLabelText(label);
1195 dialog->setTextValue(text);
1196 dialog->setInputMethodHints(inputMethodHints);
1197
1198 const int ret = dialog->exec();
1199 if (ok)
1200 *ok = !!ret;
1201 if (bool(dialog) && ret) {
1202 return dialog->textValue();
1203 } else {
1204 return QString();
1205 }
1206}
1207
1208/*!
1209 Static convenience function to get an integer input from the user.
1210
1211 \a title is the text which is displayed in the title bar of the dialog.
1212 \a label is the text which is shown to the user (it should say what should
1213 be entered).
1214 \a value is the default integer which the spinbox will be set to.
1215 \a min and \a max are the minimum and maximum values the user may choose.
1216 \a step is the amount by which the values change as the user presses the
1217 arrow buttons to increment or decrement the value.
1218
1219 If \a ok is nonnull *\a ok will be set to true if the user pressed \uicontrol OK
1220 and to false if the user pressed \uicontrol Cancel. The dialog's parent is
1221 \a parent. The dialog will be modal and uses the widget \a flags.
1222
1223 On success, this function returns the integer which has been entered by the
1224 user; on failure, it returns the initial \a value.
1225
1226 Use this static function like this:
1227
1228 \snippet dialogs/standarddialogs/dialog.cpp 0
1229
1230 \sa getText(), getDouble(), getItem(), getMultiLineText()
1231*/
1232
1233int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &label, int value,
1234 int min, int max, int step, bool *ok, Qt::WindowFlags flags)
1235{
1236 QAutoPointer<QInputDialog> dialog(new QInputDialog(parent, flags));
1237 dialog->setWindowTitle(title);
1238 dialog->setLabelText(label);
1239 dialog->setIntRange(min, max);
1240 dialog->setIntValue(value);
1241 dialog->setIntStep(step);
1242
1243 const int ret = dialog->exec();
1244 if (ok)
1245 *ok = !!ret;
1246 if (bool(dialog) && ret) {
1247 return dialog->intValue();
1248 } else {
1249 return value;
1250 }
1251}
1252
1253/*!
1254 Static convenience function to get a floating point number from the user.
1255
1256 \a title is the text which is displayed in the title bar of the dialog.
1257 \a label is the text which is shown to the user (it should say what should
1258 be entered).
1259 \a value is the default floating point number that the line edit will be
1260 set to.
1261 \a min and \a max are the minimum and maximum values the user may choose.
1262 \a decimals is the maximum number of decimal places the number may have.
1263 \a step is the amount by which the values change as the user presses the
1264 arrow buttons to increment or decrement the value.
1265
1266 If \a ok is nonnull, *\a ok will be set to true if the user pressed \uicontrol OK
1267 and to false if the user pressed \uicontrol Cancel. The dialog's parent is
1268 \a parent. The dialog will be modal and uses the widget \a flags.
1269
1270 This function returns the floating point number which has been entered by
1271 the user.
1272
1273 Use this static function like this:
1274
1275 \snippet dialogs/standarddialogs/dialog.cpp 1
1276
1277 \sa getText(), getInt(), getItem(), getMultiLineText()
1278*/
1279
1280double QInputDialog::getDouble(QWidget *parent, const QString &title, const QString &label,
1281 double value, double min, double max, int decimals, bool *ok,
1282 Qt::WindowFlags flags, double step)
1283{
1284 QAutoPointer<QInputDialog> dialog(new QInputDialog(parent, flags));
1285 dialog->setWindowTitle(title);
1286 dialog->setLabelText(label);
1287 dialog->setDoubleDecimals(decimals);
1288 dialog->setDoubleRange(min, max);
1289 dialog->setDoubleValue(value);
1290 dialog->setDoubleStep(step);
1291
1292 const int ret = dialog->exec();
1293 if (ok)
1294 *ok = !!ret;
1295 if (bool(dialog) && ret) {
1296 return dialog->doubleValue();
1297 } else {
1298 return value;
1299 }
1300}
1301
1302/*!
1303 Static convenience function to let the user select an item from a string
1304 list.
1305
1306 \a title is the text which is displayed in the title bar of the dialog.
1307 \a label is the text which is shown to the user (it should say what should
1308 be entered).
1309 \a items is the string list which is inserted into the combo box.
1310 \a current is the number of the item which should be the current item.
1311 \a inputMethodHints is the input method hints that will be used if the
1312 combo box is editable and an input method is active.
1313
1314 If \a editable is true the user can enter their own text; otherwise, the
1315 user may only select one of the existing items.
1316
1317 If \a ok is nonnull \e {*ok} will be set to true if the user pressed
1318 \uicontrol OK and to false if the user pressed \uicontrol Cancel. The dialog's parent
1319 is \a parent. The dialog will be modal and uses the widget \a flags.
1320
1321 This function returns the text of the current item, or if \a editable is
1322 true, the current text of the combo box.
1323
1324 Use this static function like this:
1325
1326 \snippet dialogs/standarddialogs/dialog.cpp 2
1327
1328 \sa getText(), getInt(), getDouble(), getMultiLineText()
1329*/
1330
1331QString QInputDialog::getItem(QWidget *parent, const QString &title, const QString &label,
1332 const QStringList &items, int current, bool editable, bool *ok,
1333 Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
1334{
1335 QString text(items.value(current));
1336
1337 QAutoPointer<QInputDialog> dialog(new QInputDialog(parent, flags));
1338 dialog->setWindowTitle(title);
1339 dialog->setLabelText(label);
1340 dialog->setComboBoxItems(items);
1341 dialog->setTextValue(text);
1342 dialog->setComboBoxEditable(editable);
1343 dialog->setInputMethodHints(inputMethodHints);
1344
1345 const int ret = dialog->exec();
1346 if (ok)
1347 *ok = !!ret;
1348 if (bool(dialog) && ret) {
1349 return dialog->textValue();
1350 } else {
1351 return text;
1352 }
1353}
1354
1355/*!
1356 \property QInputDialog::doubleStep
1357 \since 5.10
1358 \brief the step by which the double value is increased and decreased
1359
1360 This property is only relevant when the input dialog is used in
1361 DoubleInput mode.
1362*/
1363
1364void QInputDialog::setDoubleStep(double step)
1365{
1366 Q_D(QInputDialog);
1367 d->ensureDoubleSpinBox();
1368 d->doubleSpinBox->setSingleStep(step);
1369}
1370
1371double QInputDialog::doubleStep() const
1372{
1373 Q_D(const QInputDialog);
1374 if (d->doubleSpinBox)
1375 return d->doubleSpinBox->singleStep();
1376 else
1377 return 1.0;
1378}
1379
1380/*!
1381 \fn void QInputDialog::doubleValueChanged(double value)
1382
1383 This signal is emitted whenever the double value changes in the dialog.
1384 The current value is specified by \a value.
1385
1386 This signal is only relevant when the input dialog is used in
1387 DoubleInput mode.
1388*/
1389
1390/*!
1391 \fn void QInputDialog::doubleValueSelected(double value)
1392
1393 This signal is emitted whenever the user selects a double value by
1394 accepting the dialog; for example, by clicking the \uicontrol{OK} button.
1395 The selected value is specified by \a value.
1396
1397 This signal is only relevant when the input dialog is used in
1398 DoubleInput mode.
1399*/
1400
1401/*!
1402 \fn void QInputDialog::intValueChanged(int value)
1403
1404 This signal is emitted whenever the integer value changes in the dialog.
1405 The current value is specified by \a value.
1406
1407 This signal is only relevant when the input dialog is used in
1408 IntInput mode.
1409*/
1410
1411/*!
1412 \fn void QInputDialog::intValueSelected(int value)
1413
1414 This signal is emitted whenever the user selects a integer value by
1415 accepting the dialog; for example, by clicking the \uicontrol{OK} button.
1416 The selected value is specified by \a value.
1417
1418 This signal is only relevant when the input dialog is used in
1419 IntInput mode.
1420*/
1421
1422/*!
1423 \fn void QInputDialog::textValueChanged(const QString &text)
1424
1425 This signal is emitted whenever the text string changes in the dialog.
1426 The current string is specified by \a text.
1427
1428 This signal is only relevant when the input dialog is used in
1429 TextInput mode.
1430*/
1431
1432/*!
1433 \fn void QInputDialog::textValueSelected(const QString &text)
1434
1435 This signal is emitted whenever the user selects a text string by
1436 accepting the dialog; for example, by clicking the \uicontrol{OK} button.
1437 The selected string is specified by \a text.
1438
1439 This signal is only relevant when the input dialog is used in
1440 TextInput mode.
1441*/
1442
1443QT_END_NAMESPACE
1444
1445#include "qinputdialog.moc"
1446#include "moc_qinputdialog.cpp"
QInputDialogListView(QWidget *parent=nullptr)
QVariant inputMethodQuery(Qt::InputMethodQuery query) const override
\reimp
void textChanged(const QString &text)
void ensureLayout() const
void setComboBoxText(const QString &text)
QVBoxLayout * mainLayout
void setInputWidget(QWidget *widget)
QPointer< QObject > receiverToDisconnectOnClose
QDoubleSpinBox * doubleSpinBox
bool useComboBoxOrListView() const
void ensureEnabledConnection(QAbstractSpinBox *spinBox)
QDialogButtonBox * buttonBox
void setListViewText(const QString &text)
QPlainTextEdit * plainTextEdit
QByteArray memberToDisconnectOnClose
void currentRowChanged(const QModelIndex &newIndex, const QModelIndex &oldIndex)
QString listViewText() const
QInputDialogListView * listView
static const char * signalForMember(const char *member)
CandidateSignal
@ NumCandidateSignals
@ DoubleValueSelectedSignal
@ IntValueSelectedSignal
@ TextValueSelectedSignal
static const char * candidateSignal(int which)