📄 qlineedit.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 "qlineedit.h"#include "qlineedit_p.h"#ifndef QT_NO_LINEEDIT#include "qaction.h"#include "qapplication.h"#include "qclipboard.h"#include "qdrag.h"#include "qdrawutil.h"#include "qevent.h"#include "qfontmetrics.h"#include "qmenu.h"#include "qpainter.h"#include "qpixmap.h"#include "qpointer.h"#include "qstringlist.h"#include "qstyle.h"#include "qstyleoption.h"#include "qtimer.h"#include "qvalidator.h"#include "qvariant.h"#include "qvector.h"#include "qwhatsthis.h"#include "qdebug.h"#include "qtextedit.h"#include <private/qtextedit_p.h>#include <private/qinternal_p.h>#ifndef QT_NO_ACCESSIBILITY#include "qaccessible.h"#endif#ifndef QT_NO_IM#include "qinputcontext.h"#include "qlist.h"#endif#ifndef QT_NO_SHORTCUT#include "qkeysequence.h"#define ACCEL_KEY(k) "\t" + QString(QKeySequence(Qt::CTRL | Qt::Key_ ## k))#else#define ACCEL_KEY(k) "\t" + QString("Ctrl+" #k)#endif#ifdef Q_WS_MACextern void qt_mac_secure_keyboard(bool); //qapplication_mac.cpp#endif#include <limits.h>#define verticalMargin 1#define horizontalMargin 2QStyleOptionFrame QLineEditPrivate::getStyleOption() const{ Q_Q(const QLineEdit); QStyleOptionFrame opt; opt.init(q); opt.lineWidth = frame ? q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth) : 0; opt.midLineWidth = 0; opt.state |= QStyle::State_Sunken; if (readOnly) opt.state |= QStyle::State_ReadOnly;#ifdef QT_KEYPAD_NAVIGATION if (q->hasEditFocus()) opt.state |= QStyle::State_HasEditFocus;#endif return opt;}/*! \class QLineEdit \brief The QLineEdit widget is a one-line text editor. \ingroup basic \mainclass A line edit allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop. By changing the echoMode() of a line edit, it can also be used as a "write-only" field, for inputs such as passwords. The length of the text can be constrained to maxLength(). The text can be arbitrarily constrained using a validator() or an inputMask(), or both. A related class is QTextEdit which allows multi-line, rich text editing. You can change the text with setText() or insert(). The text is retrieved with text(); the displayed text (which may be different, see \l{EchoMode}) is retrieved with displayText(). Text can be selected with setSelection() or selectAll(), and the selection can be cut(), copy()ied and paste()d. The text can be aligned with setAlignment(). When the text changes the textChanged() signal is emitted; when the text changes other than by calling setText() the textEdited() signal is emitted; when the cursor is moved the cursorPositionChanged() signal is emitted; and when the Return or Enter key is pressed the returnPressed() signal is emitted. When editing is finished, either because the line edit lost focus or Return/Enter is pressed the editingFinished() signal is emitted. Note that if there is a validator set on the line edit, the returnPressed()/editingFinished() signals will only be emitted if the validator returns QValidator::Acceptable. By default, QLineEdits have a frame as specified by the Windows and Motif style guides; you can turn it off by calling setFrame(false). The default key bindings are described below. The line edit also provides a context menu (usually invoked by a right mouse click) that presents some of these editing options. \target desc \table \header \i Keypress \i Action \row \i Left Arrow \i Moves the cursor one character to the left. \row \i Shift+Left Arrow \i Moves and selects text one character to the left. \row \i Right Arrow \i Moves the cursor one character to the right. \row \i Shift+Right Arrow \i Moves and selects text one character to the right. \row \i Home \i Moves the cursor to the beginning of the line. \row \i End \i Moves the cursor to the end of the line. \row \i Backspace \i Deletes the character to the left of the cursor. \row \i Ctrl+Backspace \i Deletes the word to the left of the cursor. \row \i Delete \i Deletes the character to the right of the cursor. \row \i Ctrl+Delete \i Deletes the word to the right of the cursor. \row \i Ctrl+A \i Moves the cursor to the beginning of the line. \row \i Ctrl+B \i Moves the cursor one character to the left. \row \i Ctrl+C \i Copies the selected text to the clipboard. \row \i Ctrl+Insert \i Copies the selected text to the clipboard. \row \i Ctrl+D \i Deletes the character to the right of the cursor. \row \i Ctrl+E \i Moves the cursor to the end of the line. \row \i Ctrl+F \i Moves the cursor one character to the right. \row \i Ctrl+H \i Deletes the character to the left of the cursor. \row \i Ctrl+K \i Deletes to the end of the line. \row \i Ctrl+V \i Pastes the clipboard text into line edit. \row \i Shift+Insert \i Pastes the clipboard text into line edit. \row \i Ctrl+X \i Deletes the selected text and copies it to the clipboard. \row \i Shift+Delete \i Deletes the selected text and copies it to the clipboard. \row \i Ctrl+Z \i Undoes the last operation. \row \i Ctrl+Y \i Redoes the last undone operation. \endtable Any other key sequence that represents a valid character, will cause the character to be inserted into the line edit. \table 100% \row \o \inlineimage macintosh-lineedit.png Screenshot of a Macintosh style line edit \o A line edit shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. \row \o \inlineimage windows-lineedit.png Screenshot of a Windows XP style line edit \o A line edit shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}. \row \o \inlineimage plastique-lineedit.png Screenshot of a Plastique style line edit \o A line edit shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}. \endtable \sa QTextEdit, QLabel, QComboBox, {fowler}{GUI Design Handbook: Field, Entry}*//*! \fn void QLineEdit::textChanged(const QString &text) This signal is emitted whenever the text changes. The \a text argument is the new text. Unlike textEdited(), this signal is also emitted when programmatically setting the text via setText().*//*! \fn void QLineEdit::textEdited(const QString &text) This signal is emitted whenever the text is edited. The \a text argument is the next text. Unlike textChanged(), this signal is \e not emitted when programmatically setting the text via setText().*//*! \fn void QLineEdit::cursorPositionChanged(int old, int new) This signal is emitted whenever the cursor moves. The previous position is given by \a old, and the new position by \a new. \sa setCursorPosition(), cursorPosition()*//*! \fn void QLineEdit::selectionChanged() This signal is emitted whenever the selection changes. \sa hasSelectedText(), selectedText()*//*! Constructs a line edit with no text. The maximum text length is set to 32767 characters. The \a parent argument is sent to the QWidget constructor. \sa setText(), setMaxLength()*/QLineEdit::QLineEdit(QWidget* parent) : QWidget(*new QLineEditPrivate, parent,0){ Q_D(QLineEdit); d->init(QString());}/*! Constructs a line edit containing the text \a contents. The cursor position is set to the end of the line and the maximum text length to 32767 characters. The \a parent and argument is sent to the QWidget constructor. \sa text(), setMaxLength()*/QLineEdit::QLineEdit(const QString& contents, QWidget* parent) : QWidget(*new QLineEditPrivate, parent, 0){ Q_D(QLineEdit); d->init(contents);}#ifdef QT3_SUPPORT/*! Constructs a line edit with no text. The maximum text length is set to 32767 characters. The \a parent and \a name arguments are sent to the QWidget constructor. \sa setText(), setMaxLength()*/QLineEdit::QLineEdit(QWidget* parent, const char* name) : QWidget(*new QLineEditPrivate, parent,0){ Q_D(QLineEdit); setObjectName(QString::fromAscii(name)); d->init(QString());}/*! Constructs a line edit containing the text \a contents. The cursor position is set to the end of the line and the maximum text length to 32767 characters. The \a parent and \a name arguments are sent to the QWidget constructor. \sa text(), setMaxLength()*/QLineEdit::QLineEdit(const QString& contents, QWidget* parent, const char* name) : QWidget(*new QLineEditPrivate, parent, 0){ Q_D(QLineEdit); setObjectName(QString::fromAscii(name)); d->init(contents);}/*! Constructs a line edit with an input \a inputMask and the text \a contents. The cursor position is set to the end of the line and the maximum text length is set to the length of the mask (the number of mask characters and separators). The \a parent and \a name arguments are sent to the QWidget constructor. \sa setMask() text()*/QLineEdit::QLineEdit(const QString& contents, const QString &inputMask, QWidget* parent, const char* name) : QWidget(*new QLineEditPrivate, parent, 0){ Q_D(QLineEdit); setObjectName(QString::fromAscii(name)); d->parseInputMask(inputMask); if (d->maskData) { QString ms = d->maskString(0, contents); d->init(ms + d->clearString(ms.length(), d->maxLength - ms.length())); d->cursor = d->nextMaskBlank(ms.length()); } else { d->init(contents); }}#endif/*! Destroys the line edit.*/QLineEdit::~QLineEdit(){}/*! \property QLineEdit::text \brief the line edit's text Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the \l modified property to false. The text is not validated when inserted with setText(). The text is truncated to maxLength() length. \sa insert(), clear()*/QString QLineEdit::text() const{ Q_D(const QLineEdit); QString res = d->text; if (d->maskData) res = d->stripString(d->text); return (res.isNull() ? QString::fromLatin1("") : res);}void QLineEdit::setText(const QString& text){ Q_D(QLineEdit); d->setText(text, -1, false);#ifdef QT_KEYPAD_NAVIGATION d->origText = d->text;#endif}/*! \property QLineEdit::displayText \brief the displayed text If \l echoMode is \l Normal this returns the same as text(); if \l EchoMode is \l Password it returns a string of asterisks text().length() characters long, e.g. "******"; if \l EchoMode is \l NoEcho returns an empty string, "". \sa setEchoMode() text() EchoMode*/QString QLineEdit::displayText() const{ Q_D(const QLineEdit); if (d->echoMode == NoEcho) return QString::fromLatin1(""); QString res = d->text;#ifdef Q_WS_QWS if (d->echoMode == Password || d->echoMode == PasswordEchoOnEdit) {#else if (d->echoMode == Password) {#endif QStyleOptionFrame opt = d->getStyleOption(); res.fill(style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter, &opt, this)); } return (res.isNull() ? QString::fromLatin1("") : res);}/*! \property QLineEdit::maxLength \brief the maximum permitted length of the text If the text is too long, it is truncated at the limit. If truncation occurs any selected text will be unselected, the cursor position is set to 0 and the first part of the string is shown. If the line edit has an input mask, the mask defines the maximum string length. \sa inputMask*/int QLineEdit::maxLength() const{ Q_D(const QLineEdit); return d->maxLength;}void QLineEdit::setMaxLength(int maxLength){ Q_D(QLineEdit); if (d->maskData) return; d->maxLength = maxLength; setText(d->text);}/*! \property QLineEdit::frame \brief whether the line edit draws itself with a frame If enabled (the default) the line edit draws itself inside a frame, otherwise the line edit draws itself without any frame.*/bool QLineEdit::hasFrame() const{ Q_D(const QLineEdit); return d->frame;}void QLineEdit::setFrame(bool enable){ Q_D(QLineEdit); d->frame = enable; update(); updateGeometry();}/*! \enum QLineEdit::EchoMode This enum type describes how a line edit should display its contents. \value Normal Display characters as they are entered. This is the default. \value NoEcho Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret. \value Password Display asterisks instead of the characters
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -