📄 qlineedit.cpp
字号:
}void QLineEdit::setReadOnly(bool enable){ Q_D(QLineEdit); if (d->readOnly != enable) { d->readOnly = enable; setAttribute(Qt::WA_MacShowFocusRect, !d->readOnly); update(); }}#ifndef QT_NO_CLIPBOARD/*! Copies the selected text to the clipboard and deletes it, if there is any, and if echoMode() is \l Normal. If the current validator disallows deleting the selected text, cut() will copy without deleting. \sa copy() paste() setValidator()*/void QLineEdit::cut(){ if (hasSelectedText()) { copy(); del(); }}/*! Copies the selected text to the clipboard, if there is any, and if echoMode() is \l Normal. \sa cut() paste()*/void QLineEdit::copy() const{ Q_D(const QLineEdit); d->copy();}/*! Inserts the clipboard's text at the cursor position, deleting any selected text, providing the line edit is not \link QLineEdit::readOnly read-only\endlink. If the end result would not be acceptable to the current \link setValidator() validator\endlink, nothing happens. \sa copy() cut()*/void QLineEdit::paste(){ if(echoMode() == PasswordEchoOnEdit) { Q_D(QLineEdit); setEchoMode(Normal); clear(); d->resumePassword = true; } insert(QApplication::clipboard()->text(QClipboard::Clipboard));}void QLineEditPrivate::copy(bool clipboard) const{ Q_Q(const QLineEdit); QString t = q->selectedText(); if (!t.isEmpty() && echoMode == QLineEdit::Normal) { q->disconnect(QApplication::clipboard(), SIGNAL(selectionChanged()), q, 0); QApplication::clipboard()->setText(t, clipboard ? QClipboard::Clipboard : QClipboard::Selection); q->connect(QApplication::clipboard(), SIGNAL(selectionChanged()), q, SLOT(_q_clipboardChanged())); }}#endif // !QT_NO_CLIPBOARD/*! \reimp*/bool QLineEdit::event(QEvent * e){ Q_D(QLineEdit); if (e->type() == QEvent::ShortcutOverride && !d->readOnly) { QKeyEvent* ke = (QKeyEvent*) e; if (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier || ke->modifiers() == Qt::KeypadModifier) { if (ke->key() < Qt::Key_Escape) { ke->accept(); } else { switch (ke->key()) { case Qt::Key_Delete: case Qt::Key_Home: case Qt::Key_End: case Qt::Key_Backspace: case Qt::Key_Left: case Qt::Key_Right: ke->accept(); default: break; } } } else if (ke->modifiers() & Qt::ControlModifier) { switch (ke->key()) {// Those are too frequently used for application functionality/* case Qt::Key_A: case Qt::Key_B: case Qt::Key_D: case Qt::Key_E: case Qt::Key_F: case Qt::Key_H: case Qt::Key_K:*/ case Qt::Key_C: case Qt::Key_V: case Qt::Key_X: case Qt::Key_Y: case Qt::Key_Z: case Qt::Key_Left: case Qt::Key_Right:#if !defined(Q_WS_MAC) case Qt::Key_Insert: case Qt::Key_Delete:#endif ke->accept(); default: break; } } } else if (e->type() == QEvent::Timer) { // should be timerEvent, is here for binary compatibility int timerId = ((QTimerEvent*)e)->timerId(); if (timerId == d->cursorTimer) { QStyleOptionFrameV2 opt; initStyleOption(&opt); if(!hasSelectedText() || style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this)) d->setCursorVisible(!d->cursorVisible);#ifndef QT_NO_DRAGANDDROP } else if (timerId == d->dndTimer.timerId()) { d->drag();#endif } else if (timerId == d->tripleClickTimer.timerId()) d->tripleClickTimer.stop();#ifdef QT_KEYPAD_NAVIGATION else if (timerId == d->deleteAllTimer.timerId()) { d->deleteAllTimer.stop(); clear(); }#endif } else if (e->type() == QEvent::ContextMenu) {#ifndef QT_NO_IM if (d->composeMode()) return true;#endif d->separate(); }#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) { if ((e->type() == QEvent::KeyPress) || (e->type() == QEvent::KeyRelease)) { QKeyEvent *ke = (QKeyEvent *)e; if (ke->key() == Qt::Key_Back) { if (ke->isAutoRepeat()) { // Swallow it. We don't want back keys running amok. ke->accept(); return true; } if ((e->type() == QEvent::KeyRelease) && !isReadOnly() && d->deleteAllTimer.isActive()) { d->deleteAllTimer.stop(); backspace(); ke->accept(); return true; } } } else if (e->type() == QEvent::EnterEditFocus) { end(false); if (!d->cursorTimer) { int cft = QApplication::cursorFlashTime(); d->cursorTimer = cft ? startTimer(cft/2) : -1; } } else if (e->type() == QEvent::LeaveEditFocus) { d->setCursorVisible(false); if (d->cursorTimer > 0) killTimer(d->cursorTimer); d->cursorTimer = 0; } }#endif return QWidget::event(e);}/*! \reimp*/void QLineEdit::mousePressEvent(QMouseEvent* e){ Q_D(QLineEdit); if (d->sendMouseEventToInputContext(e)) return; if (e->button() == Qt::RightButton) return;#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { setEditFocus(true); // Get the completion list to pop up. if (d->completer) d->completer->complete(); }#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);#ifndef QT_NO_COMPLETER if (d->completer && d->completer->popup() && d->completer->popup()->isVisible()) { // The following keys are forwarded by the completer to the widget // Ignoring the events lets the completer provide suitable default behavior switch (event->key()) { case Qt::Key_Escape: event->ignore(); return; case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_F4:#ifdef QT_KEYPAD_NAVIGATION case Qt::Key_Select: if (!QApplication::keypadNavigationEnabled()) break;#endif d->completer->popup()->hide(); // just hide. will end up propagating to parent default: break; // normal key processing } }#endif // QT_NO_COMPLETER#ifdef QT_KEYPAD_NAVIGATION bool select = false; switch (event->key()) { case Qt::Key_Select: if (QApplication::keypadNavigationEnabled()) { if (hasEditFocus()) { setEditFocus(false); if (d->completer && d->completer->popup()->isVisible()) d->completer->popup()->hide(); 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() && !isReadOnly()) { setEditFocus(true); clear(); } else { event->ignore(); return; } } } } if (QApplication::keypadNavigationEnabled() && !select && !hasEditFocus()) { setEditFocus(true); if (event->key() == Qt::Key_Select) return; // Just start. No action. }#endif if(echoMode() == PasswordEchoOnEdit && !isReadOnly() && !event->text().isEmpty() &&#ifdef QT_KEYPAD_NAVIGATION event->key() != Qt::Key_Select && event->key() != Qt::Key_Up && event->key() != Qt::Key_Down && event->key() != Qt::Key_Back &&#endif !(event->modifiers() & Qt::ControlModifier)) { setEchoMode(Normal); clear(); d->resumePassword = true; } d->setCursorVisible(true); if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { if (hasAcceptableInput() || d->fixup()) { emit returnPressed(); d->emitingEditingFinished = true; emit editingFinished(); d->emitingEditingFinished = false; } event->ignore(); return; } bool unknown = false; if (false) { }#ifndef QT_NO_SHORTCUT else if (event == QKeySequence::Undo) { if (!d->readOnly) undo(); } else if (event == QKeySequence::Redo) { if (!d->readOnly) redo(); } else if (event == QKeySequence::SelectAll) { selectAll(); }#ifndef QT_NO_CLIPBOARD else if (event == QKeySequence::Copy) { copy();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -