📄 qlineedit.cpp
字号:
}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 actually entered. \value PasswordEchoOnEdit Display characters as they are entered while editing otherwise display asterisks. \sa setEchoMode() echoMode()*//*! \property QLineEdit::echoMode \brief the line edit's echo mode The initial setting is \l Normal, but QLineEdit also supports \l NoEcho, \l Password and \l PasswordEchoOnEdit modes. The widget's display and the ability to copy or drag the text is affected by this setting. \sa EchoMode displayText()*/QLineEdit::EchoMode QLineEdit::echoMode() const{ Q_D(const QLineEdit); return (EchoMode) d->echoMode;}void QLineEdit::setEchoMode(EchoMode mode){ Q_D(QLineEdit); if(mode == (EchoMode)d->echoMode) return; setAttribute(Qt::WA_InputMethodEnabled, mode == Normal || mode == PasswordEchoOnEdit); d->echoMode = mode; d->updateTextLayout(); update();#ifdef Q_WS_MAC if (hasFocus()) qt_mac_secure_keyboard(d->echoMode == Password || d->echoMode == NoEcho);#endif}#ifndef QT_NO_VALIDATOR/*! Returns a pointer to the current input validator, or 0 if no validator has been set. \sa setValidator()*/const QValidator * QLineEdit::validator() const{ Q_D(const QLineEdit); return d->validator;}/*! Sets this line edit to only accept input that the validator, \a v, will accept. This allows you to place any arbitrary constraints on the text which may be entered. If \a v == 0, setValidator() removes the current input validator. The initial setting is to have no input validator (i.e. any input is accepted up to maxLength()). \sa validator() QIntValidator QDoubleValidator QRegExpValidator*/void QLineEdit::setValidator(const QValidator *v){ Q_D(QLineEdit); d->validator = const_cast<QValidator*>(v);}#endif // QT_NO_VALIDATOR#ifndef QT_NO_COMPLETER/*! \since 4.2 Sets this line edit to provide auto completions from the completer, \a c. The completion mode is set using QCompleter::setCompletionMode(). To use a QCompleter with a QValidator or QLineEdit::inputMask, you need to ensure that the model provided to QCompleter contains valid entries. You can use the QSortFilterProxyModel to ensure that the QCompleter's model contains only valid entries. If \a c == 0, setCompleter() removes the current completer, effectively disabling auto completion. \sa QCompleter*/void QLineEdit::setCompleter(QCompleter *c){ Q_D(QLineEdit); if (c == d->completer) return; if (d->completer) { disconnect(d->completer, 0, this, 0); d->completer->setWidget(0); if (d->completer->parent() == this) delete d->completer; } d->completer = c; if (!c) return; if (c->widget() == 0) c->setWidget(this); if (hasFocus()) { QObject::connect(d->completer, SIGNAL(activated(QString)), this, SLOT(setText(QString))); QObject::connect(d->completer, SIGNAL(highlighted(QString)), this, SLOT(_q_completionHighlighted(QString))); }}/*! \since 4.2 Returns the current QCompleter that provides completions.*/QCompleter *QLineEdit::completer() const{ Q_D(const QLineEdit); return d->completer;}// looks for an enabled item iterating forward(dir=1)/backward(dir=-1) from the// current row based. dir=0 indicates a new completion prefix was set.bool QLineEditPrivate::advanceToEnabledItem(int dir){ int start = completer->currentRow(); if (start == -1) return false; int i = start + dir; if (dir == 0) dir = 1; do { if (!completer->setCurrentRow(i)) { if (!completer->wrapAround()) break; i = i > 0 ? 0 : completer->completionCount() - 1; } else { QModelIndex currentIndex = completer->currentIndex(); if (completer->completionModel()->flags(currentIndex) & Qt::ItemIsEnabled) return true; i += dir; } } while (i != start); completer->setCurrentRow(start); // restore return false;}void QLineEditPrivate::complete(int key){ if (!completer || readOnly || echoMode != QLineEdit::Normal) return; if (completer->completionMode() == QCompleter::InlineCompletion) { if (key == Qt::Key_Backspace) return; int n = 0; if (key == Qt::Key_Up || key == Qt::Key_Down) { if (selend != 0 && selend != text.length()) return; QString prefix = hasSelectedText() ? text.left(selstart) : text; if (text.compare(completer->currentCompletion(), completer->caseSensitivity()) != 0 || prefix.compare(completer->completionPrefix(), completer->caseSensitivity()) != 0) { completer->setCompletionPrefix(prefix); } else { n = (key == Qt::Key_Up) ? -1 : +1; } } else { completer->setCompletionPrefix(text); } if (!advanceToEnabledItem(n)) return; } else {#ifndef QT_KEYPAD_NAVIGATION if (text.isEmpty()) { completer->popup()->hide(); return; }#endif completer->setCompletionPrefix(text); } completer->complete();}void QLineEditPrivate::_q_completionHighlighted(QString newText){ Q_Q(QLineEdit); if (completer->completionMode() != QCompleter::InlineCompletion) q->setText(newText); else { int c = cursor; q->setText(text.left(c) + newText.mid(c)); q->setSelection(text.length(), c - newText.length()); }}#endif // QT_NO_COMPLETER/*! Returns a recommended size for the widget. The width returned, in pixels, is usually enough for about 15 to 20 characters.*/QSize QLineEdit::sizeHint() const{ Q_D(const QLineEdit); ensurePolished(); QFontMetrics fm(font()); int h = qMax(fm.lineSpacing(), 14) + 2*verticalMargin + d->topmargin + d->bottommargin; int w = fm.width(QLatin1Char('x')) * 17 + 2*horizontalMargin + d->leftmargin + d->rightmargin; // "some" QStyleOptionFrameV2 opt; initStyleOption(&opt); return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h). expandedTo(QApplication::globalStrut()), this));}/*! Returns a minimum size for the line edit. The width returned is enough for at least one character.*/QSize QLineEdit::minimumSizeHint() const{ Q_D(const QLineEdit); ensurePolished(); QFontMetrics fm = fontMetrics(); int h = fm.height() + qMax(2*verticalMargin, fm.leading()) + d->topmargin + d->bottommargin; int w = fm.maxWidth() + d->leftmargin + d->rightmargin; QStyleOptionFrameV2 opt; initStyleOption(&opt); return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h). expandedTo(QApplication::globalStrut()), this));}/*! \property QLineEdit::cursorPosition \brief the current cursor position for this line edit Setting the cursor position causes a repaint when appropriate.*/int QLineEdit::cursorPosition() const{ Q_D(const QLineEdit); return d->cursor;}void QLineEdit::setCursorPosition(int pos){ Q_D(QLineEdit); if (pos < 0) pos = 0; if (pos <= d->text.length()) d->moveCursor(pos);}/*! Returns the cursor position under the point \a pos.*/// ### What should this do if the point is outside of contentsRect? Currently returns 0.int QLineEdit::cursorPositionAt(const QPoint &pos){ Q_D(QLineEdit); return d->xToPos(pos.x());}#ifdef QT3_SUPPORT/*! \obsolete Use setText(), setCursorPosition() and setSelection() instead.*/bool QLineEdit::validateAndSet(const QString &newText, int newPos, int newMarkAnchor, int newMarkDrag){ Q_D(QLineEdit); int priorState = d->undoState; d->selstart = 0; d->selend = d->text.length(); d->removeSelectedText(); d->insert(newText); d->finishChange(priorState); if (d->undoState > priorState) { d->cursor = newPos; d->selstart = qMin(newMarkAnchor, newMarkDrag); d->selend = qMax(newMarkAnchor, newMarkDrag); update(); d->emitCursorPositionChanged(); return true; } return false;}#endif //QT3_SUPPORT/*! \property QLineEdit::alignment \brief the alignment of the line edit Both horizontal and vertical alignment is allowed here, Qt::AlignJustify will map to Qt::AlignLeft. \sa Qt::Alignment*/Qt::Alignment QLineEdit::alignment() const{ Q_D(const QLineEdit); return QFlag(d->alignment);}void QLineEdit::setAlignment(Qt::Alignment alignment){ Q_D(QLineEdit); d->alignment = alignment; update();}/*! Moves the cursor forward \a steps characters. If \a mark is true each character moved over is added to the selection; if \a mark is false the selection is cleared. \sa cursorBackward()*/void QLineEdit::cursorForward(bool mark, int steps){ Q_D(QLineEdit); int cursor = d->cursor; if (steps > 0) { while(steps--) cursor = d->textLayout.nextCursorPosition(cursor); } else if (steps < 0) { while (steps++) cursor = d->textLayout.previousCursorPosition(cursor); } d->moveCursor(cursor, mark);}/*! Moves the cursor back \a steps characters. If \a mark is true each character moved over is added to the selection; if \a mark is false the selection is cleared. \sa cursorForward()*/void QLineEdit::cursorBackward(bool mark, int steps){ cursorForward(mark, -steps);}/*! Moves the cursor one word forward. If \a mark is true, the word is also selected. \sa cursorWordBackward()*/void QLineEdit::cursorWordForward(bool mark){ Q_D(QLineEdit); d->moveCursor(d->textLayout.nextCursorPosition(d->cursor, QTextLayout::SkipWords), mark);}/*! Moves the cursor one word backward. If \a mark is true, the word is also selected. \sa cursorWordForward()*/void QLineEdit::cursorWordBackward(bool mark){ Q_D(QLineEdit); d->moveCursor(d->textLayout.previousCursorPosition(d->cursor, QTextLayout::SkipWords), mark);}/*! If no text is selected, deletes the character to the left of the text cursor and moves the cursor one position to the left. If any text is selected, the cursor is moved to the beginning of the selected text and the selected text is deleted. \sa del()*/void QLineEdit::backspace(){ Q_D(QLineEdit); int priorState = d->undoState; if (d->hasSelectedText()) { d->removeSelectedText(); } else if (d->cursor) { --d->cursor; if (d->maskData) d->cursor = d->prevMaskBlank(d->cursor); QChar uc = d->text.at(d->cursor); if (d->cursor > 0 && uc.unicode() >= 0xdc00 && uc.unicode() < 0xe000) { // second half of a surrogate, check if we have the first half as well, // if yes delete both at once uc = d->text.at(d->cursor - 1); if (uc.unicode() >= 0xd800 && uc.unicode() < 0xdc00) { d->del(true); --d->cursor; } } d->del(true); } d->finishChange(priorState);}/*! If no text is selected, deletes the character to the right of the text cursor. If any text is selected, the cursor is moved to the beginning of the selected text and the selected text is deleted. \sa backspace()*/void QLineEdit::del(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -