📄 qinputdialog.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qinputdialog.h"#ifndef QT_NO_INPUTDIALOG#include "qapplication.h"#include "qcombobox.h"#include "qdialogbuttonbox.h"#include "qlabel.h"#include "qlayout.h"#include "qlineedit.h"#include "qpushbutton.h"#include "qspinbox.h"#include "qstackedlayout.h"#include "qvalidator.h"#include "qevent.h"#include "qdialog_p.h"// This internal class adds extra validation to a QSpinBox by emitting textChanged(bool)// after events that may potentially change the visible text. Return or Enter key presses// are not propagated if the visible text is invalid. Instead, the visible text is modified// to the last valid value.class QInputDialogValidatedSpinBox : public QSpinBox{ Q_OBJECTpublic: QInputDialogValidatedSpinBox(int minValue, int maxValue, int step, int value) : QSpinBox(0) { setRange(minValue, maxValue); setSingleStep(step); setValue(value); selectAll(); validator = new QIntValidator(minValue, maxValue, this); QObject::connect( lineEdit(), SIGNAL(textChanged(const QString &)), this, SLOT(notifyTextChanged())); QObject::connect(this, SIGNAL(editingFinished()), this, SLOT(notifyTextChanged())); }private: QIntValidator *validator; void keyPressEvent(QKeyEvent *event) { if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !textValid()) {#ifndef QT_NO_PROPERTIES setProperty("value", property("value"));#endif } else { QSpinBox::keyPressEvent(event); } notifyTextChanged(); } void mousePressEvent(QMouseEvent *event) { QSpinBox::mousePressEvent(event); notifyTextChanged(); } bool textValid() const { QString t = text(); int pos = 0; return validate(t, pos) == QValidator::Acceptable; }private slots: void notifyTextChanged() { emit textChanged(textValid()); }signals: void textChanged(bool);};// This internal class adds extra validation to a QDoubleSpinBox by emitting textChanged(bool)// after events that may potentially change the visible text. Return or Enter key presses// are not propagated if the visible text is invalid. Instead, the visible text is modified// to the last valid value.class QInputDialogValidatedDoubleSpinBox : public QDoubleSpinBox{ Q_OBJECTpublic: QInputDialogValidatedDoubleSpinBox( double minValue, double maxValue, int decimals, double value) : QDoubleSpinBox(0) { setDecimals(decimals); setRange(minValue, maxValue); setValue(value); selectAll(); validator = new QDoubleValidator(minValue, maxValue, decimals, this); QObject::connect( lineEdit(), SIGNAL(textChanged(const QString &)), this, SLOT(notifyTextChanged())); QObject::connect(this, SIGNAL(editingFinished()), this, SLOT(notifyTextChanged())); }private: QDoubleValidator *validator; void keyPressEvent(QKeyEvent *event) { if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && !textValid()) {#ifndef QT_NO_PROPERTIES setProperty("value", property("value"));#endif } else { QDoubleSpinBox::keyPressEvent(event); } notifyTextChanged(); } void mousePressEvent(QMouseEvent *event) { QDoubleSpinBox::mousePressEvent(event); notifyTextChanged(); } bool textValid() const { QString t = text(); int pos = 0; return validate(t, pos) == QValidator::Acceptable; }private slots: void notifyTextChanged() { emit textChanged(textValid()); }signals: void textChanged(bool);};#include "qinputdialog.moc"class QInputDialogPrivate : public QDialogPrivate{ Q_DECLARE_PUBLIC(QInputDialog)public: QInputDialogPrivate(); QLabel *label; // ### Qt 5: remove QPushButton *okButton; QWidget *input; // ### Qt 5: remove void init(const QString &label, QInputDialog::Type); // ### Qt 5: remove void init(const QString &title, const QString &label, QWidget *input); void tryAccept();};QInputDialogPrivate::QInputDialogPrivate() : QDialogPrivate(){}// ### Qt 5: removevoid QInputDialogPrivate::init(const QString &lbl, QInputDialog::Type type){ Q_Q(QInputDialog); QVBoxLayout *vbox = new QVBoxLayout(q); label = new QLabel(lbl, q); vbox->addWidget(label); vbox->addStretch(1); switch (type) { case QInputDialog::LineEdit: input = new QLineEdit(q); break; case QInputDialog::SpinBox: input = new QSpinBox(q); break; case QInputDialog::DoubleSpinBox: input = new QDoubleSpinBox(q); break; case QInputDialog::ComboBox: case QInputDialog::EditableComboBox: { QComboBox *combo = new QComboBox(q); if (type == QInputDialog::EditableComboBox) combo->setEditable(true); input = combo; } break; } vbox->addWidget(input); vbox->addStretch(1);#ifndef QT_NO_SHORTCUT label->setBuddy(input);#endif QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, q); buttonBox->setObjectName(QLatin1String("qt_inputdlg_buttonbox")); QPushButton *okButton = static_cast<QPushButton *>(buttonBox->addButton(QDialogButtonBox::Ok)); okButton->setDefault(true); vbox->addWidget(buttonBox); QObject::connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept())); QObject::connect(buttonBox, SIGNAL(rejected()), q, SLOT(reject()));}void QInputDialogPrivate::init(const QString &title, const QString &lbl, QWidget *input){ Q_Q(QInputDialog); q->setWindowTitle(title); QVBoxLayout *vbox = new QVBoxLayout(q); QLabel *label = new QLabel(lbl, q); vbox->addWidget(label); vbox->addStretch(1); input->setParent(q); vbox->addWidget(input); vbox->addStretch(1);#ifndef QT_NO_SHORTCUT label->setBuddy(input);#endif QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, q); buttonBox->setObjectName(QLatin1String("qt_inputdlg_buttonbox")); okButton = static_cast<QPushButton *>(buttonBox->addButton(QDialogButtonBox::Ok)); okButton->setDefault(true); vbox->addWidget(buttonBox); QObject::connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept())); QObject::connect(buttonBox, SIGNAL(rejected()), q, SLOT(reject())); q->setSizeGripEnabled(true);}/*! \class QInputDialog \brief The QInputDialog class provides a simple convenience dialog to get a single value from the user. \ingroup dialogs \mainclass 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(), getInteger(), getDouble() and getItem(). All the functions can be used in a similar way, for example: \quotefromfile dialogs/standarddialogs/dialog.cpp \skipuntil Dialog::setText \skipline { \printto } The \c ok variable is set to true if the user clicks \gui OK; otherwise it is set to false. \img inputdialogs.png Input Dialogs The \l{dialogs/standarddialogs}{Standard Dialogs} example shows how to use QInputDialog as well as other built-in Qt dialogs. \sa QMessageBox, {Standard Dialogs Example}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -