📄 qlineedit.cpp
字号:
Q_D(QLineEdit); if (d->sendMouseEventToInputContext(e)) return; if (e->button() == Qt::RightButton) return;#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) setEditFocus(true);#endif if (d->tripleClickTimer.isActive() && (e->pos() - d->tripleClick).manhattanLength() < QApplication::startDragDistance()) { selectAll(); return; } bool mark = e->modifiers() & Qt::ShiftModifier; int cursor = d->xToPos(e->pos().x());#ifndef QT_NO_DRAGANDDROP if (!mark && d->dragEnabled && d->echoMode == Normal && e->button() == Qt::LeftButton && d->inSelection(e->pos().x())) { d->cursor = cursor; update(); d->dndPos = e->pos(); if (!d->dndTimer.isActive()) d->dndTimer.start(QApplication::startDragTime(), this); d->emitCursorPositionChanged(); } else#endif { d->moveCursor(cursor, mark); }}/*! \reimp*/void QLineEdit::mouseMoveEvent(QMouseEvent * e){ Q_D(QLineEdit); if (d->sendMouseEventToInputContext(e)) return; if (e->buttons() & Qt::LeftButton) {#ifndef QT_NO_DRAGANDDROP if (d->dndTimer.isActive()) { if ((d->dndPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) d->drag(); } else#endif { d->moveCursor(d->xToPos(e->pos().x()), true); } }}/*! \reimp*/void QLineEdit::mouseReleaseEvent(QMouseEvent* e){ Q_D(QLineEdit); if (d->sendMouseEventToInputContext(e)) return;#ifndef QT_NO_DRAGANDDROP if (e->button() == Qt::LeftButton) { if (d->dndTimer.isActive()) { d->dndTimer.stop(); deselect(); return; } }#endif#ifndef QT_NO_CLIPBOARD if (QApplication::clipboard()->supportsSelection()) { if (e->button() == Qt::LeftButton) { d->copy(false); } else if (!d->readOnly && e->button() == Qt::MidButton) { d->deselect(); insert(QApplication::clipboard()->text(QClipboard::Selection)); } }#endif}/*! \reimp*/void QLineEdit::mouseDoubleClickEvent(QMouseEvent* e){ Q_D(QLineEdit); if (d->sendMouseEventToInputContext(e)) return; if (e->button() == Qt::LeftButton) { deselect(); d->cursor = d->xToPos(e->pos().x()); d->cursor = d->textLayout.previousCursorPosition(d->cursor, QTextLayout::SkipWords); // ## text layout should support end of words. int end = d->textLayout.nextCursorPosition(d->cursor, QTextLayout::SkipWords); while (end > d->cursor && d->text[end-1].isSpace()) --end; d->moveCursor(end, true); d->tripleClickTimer.start(QApplication::doubleClickInterval(), this); d->tripleClick = e->pos(); }}/*! \fn void QLineEdit::returnPressed() This signal is emitted when the Return or Enter key is pressed. Note that if there is a validator() or inputMask() set on the line edit, the returnPressed() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.*//*! \fn void QLineEdit::editingFinished() This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.*//*! Converts the given key press \a event into a line edit action. If Return or Enter is pressed and the current text is valid (or can be \link QValidator::fixup() made valid\endlink by the validator), the signal returnPressed() is emitted. The default key bindings are listed in the class's detailed description.*/void QLineEdit::keyPressEvent(QKeyEvent *event){ Q_D(QLineEdit);#ifdef QT_KEYPAD_NAVIGATION bool select = false; switch (event->key()) { case Qt::Key_Select: if (QApplication::keypadNavigationEnabled()) { if (hasEditFocus()) { setEditFocus(false); select = true; } } break; case Qt::Key_Back: case Qt::Key_No: if (!QApplication::keypadNavigationEnabled() || !hasEditFocus()) { event->ignore(); return; } break; default: if (QApplication::keypadNavigationEnabled()) { if (!hasEditFocus() && !(event->modifiers() & Qt::ControlModifier)) { if (!event->text().isEmpty() && event->text().at(0).isPrint()) { setEditFocus(true); clear(); } else { event->ignore(); return; } } } }#if defined(Q_WS_QWS) if(event->key() != Qt::Key_Select && event->key() != Qt::Key_Up && event->key() != Qt::Key_Down && event->key() != Qt::Key_Back && echoMode() == PasswordEchoOnEdit && !isReadOnly()){ setEchoMode(Normal); clear(); d->resumePassword = true;}#endif if (QApplication::keypadNavigationEnabled() && !select && !hasEditFocus()) { setEditFocus(true); if (event->key() == Qt::Key_Select) return; // Just start. No action. }#endif d->setCursorVisible(true); if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { if (hasAcceptableInput()) { emit returnPressed(); d->emitingEditingFinished = true; emit editingFinished(); d->emitingEditingFinished = false; }#ifndef QT_NO_VALIDATOR else { const QValidator * v = d->validator; QString textCopy = d->text; int cursorCopy = d->cursor; if (v && v->validate(textCopy, cursorCopy) != QValidator::Acceptable) v->fixup(textCopy); if (d->hasAcceptableInput(textCopy)) { if (v && (textCopy != d->text || cursorCopy != d->cursor)) d->setText(textCopy, cursorCopy); emit returnPressed(); d->emitingEditingFinished = true; emit editingFinished(); d->emitingEditingFinished = false; } }#endif event->ignore(); return; } bool unknown = false; if (event->modifiers() & Qt::ControlModifier) { switch (event->key()) { case Qt::Key_A:#if defined(Q_WS_X11) home(event->modifiers() & Qt::ShiftModifier);#else selectAll();#endif break; case Qt::Key_B: cursorForward(event->modifiers() & Qt::ShiftModifier, -1); break;#ifndef QT_NO_CLIPBOARD case Qt::Key_C: copy(); break;#endif case Qt::Key_D: if (!d->readOnly) del(); break; case Qt::Key_E: end(event->modifiers() & Qt::ShiftModifier); break; case Qt::Key_F: cursorForward(event->modifiers() & Qt::ShiftModifier, 1); break; case Qt::Key_H: if (!d->readOnly) backspace(); break; case Qt::Key_K: if (!d->readOnly) { setSelection(d->cursor, d->text.size());#ifndef QT_NO_CLIPBOARD copy();#endif del(); } break;#if defined(Q_WS_X11) case Qt::Key_U: if (!d->readOnly) { setSelection(0, d->text.size());#ifndef QT_NO_CLIPBOARD copy();#endif del(); } break;#endif#ifndef QT_NO_CLIPBOARD case Qt::Key_V: if (!d->readOnly) paste(); break;#endif case Qt::Key_X: if (!d->readOnly) {#ifndef QT_NO_CLIPBOARD copy();#endif del(); } break;#if !defined(Q_WS_MAC) && !defined(QT_NO_CLIPBOARD) case Qt::Key_Insert: copy(); break;#endif case Qt::Key_Delete: if (!d->readOnly) { cursorWordForward(true); del(); } break; case Qt::Key_Backspace: if (!d->readOnly) { cursorWordBackward(true); del(); } break; case Qt::Key_Right: case Qt::Key_Left: if ((layoutDirection() == Qt::RightToLeft) == (event->key() == Qt::Key_Right)) {#ifndef Q_WS_MAC if (echoMode() == Normal) cursorWordBackward(event->modifiers() & Qt::ShiftModifier); else#endif home(event->modifiers() & Qt::ShiftModifier); } else {#ifndef Q_WS_MAC if (echoMode() == Normal) cursorWordForward(event->modifiers() & Qt::ShiftModifier); else#endif end(event->modifiers() & Qt::ShiftModifier); } break; case Qt::Key_Z: if (!d->readOnly) { if(event->modifiers() & Qt::ShiftModifier) redo(); else undo(); } break; case Qt::Key_Y: if (!d->readOnly) redo(); break; default: unknown = true; } } else { // ### check for *no* modifier switch (event->key()) { case Qt::Key_Shift: // ### TODO break; case Qt::Key_Left: case Qt::Key_Right: { int step = ((layoutDirection() == Qt::RightToLeft) == (event->key() == Qt::Key_Right)) ? -1 : 1;#ifdef Q_WS_MAC if (event->modifiers() & Qt::AltModifier) { if (step < 0) cursorWordBackward(event->modifiers() & Qt::ShiftModifier); else cursorWordForward(event->modifiers() & Qt::ShiftModifier); } else if (event->modifiers() & Qt::MetaModifier) { if (step < 0) home(event->modifiers() & Qt::ShiftModifier); else end(event->modifiers() & Qt::ShiftModifier); } else#endif { cursorForward(event->modifiers() & Qt::ShiftModifier, step); } } break; case Qt::Key_Backspace:#if defined(Q_WS_WIN) if (event->modifiers() & Qt::AltModifier) (event->modifiers() & Qt::ShiftModifier) ? redo() : undo(); else#endif if (!d->readOnly) backspace(); break; case Qt::Key_Home:#ifdef Q_WS_MAC break; // Home and End do nothing on the mac (but Up and Down do). case Qt::Key_Up:#endif home(event->modifiers() & Qt::ShiftModifier); break; case Qt::Key_End:#ifdef Q_WS_MAC break; case Qt::Key_Down:#endif end(event->modifiers() & Qt::ShiftModifier); break; case Qt::Key_Delete:#if !defined(QT_NO_CLIPBOARD) if (!d->readOnly) {#if !defined(Q_WS_MAC) if (event->modifiers() & Qt::ShiftModifier) { cut(); break; }#endif del(); }#endif break;#if !defined(Q_WS_MAC) && !defined(QT_NO_CLIPBOARD) case Qt::Key_Insert: if (!d->readOnly && event->modifiers() & Qt::ShiftModifier) paste(); else unknown = true; break;#endif case Qt::Key_F14: // Undo key on Sun keyboards if (!d->readOnly) undo(); break;#ifndef QT_NO_CLIPBOARD case Qt::Key_F16: // Copy key on Sun keyboards copy(); break; case Qt::Key_F18: // Paste key on Sun keyboards if (!d->readOnly) paste(); break; case Qt::Key_F20: // Cut key on Sun keyboards if (!d->readOnly) { copy(); del(); } break;#endif#ifdef QT_KEYPAD_NAVIGATION case Qt::Key_Back: if (QApplication::keypadNavigationEnabled() && !event->isAutoRepeat() && !isReadOnly()) { if (text().length() == 0) { setText(d->origText);#if defined Q_WS_QWS if(d->resumePassword) { setEchoMode(PasswordEchoOnEdit); d->resumePassword = false; }#endif setEditFocus(false); } else if (!d->deleteAllTimer.isActive()) { d->deleteAllTimer.start(750, this); } } else { unknown = true; } break;#endif default: unknown = true; } } if (event->key() == Qt::Key_Direction_L || event->key() == Qt::Key_Direction_R) { setLayoutDirection((event->key() == Qt::Key_Direction_L) ? Qt::LeftToRight : Qt::RightToLeft); d->updateTextLayout(); update(); unknown = false; } if (unknown && !d->readOnly) { QString t = event->text(); if (!t.isEmpty() && t.at(0).isPrint()) { insert(t); event->accept(); return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -