📄 qtextedit.cpp
字号:
{ Q_D(QTextEdit); d->cursor = cursor; d->_q_updateCurrentCharFormatAndSelection(); ensureCursorVisible(); d->viewport->update(); emit cursorPositionChanged();}/*! Returns a copy of the QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QTextEdit's cursor; use setTextCursor() to update the visible cursor. */QTextCursor QTextEdit::textCursor() const{ Q_D(const QTextEdit); return d->cursor;}/*! Sets the font family of the current format to \a fontFamily. \sa fontFamily() setCurrentFont()*/void QTextEdit::setFontFamily(const QString &fontFamily){ QTextCharFormat fmt; fmt.setFontFamily(fontFamily); mergeCurrentCharFormat(fmt);}/*! Sets the point size of the current format to \a s. Note that if \a s is zero or negative, the behavior of this function is not defined. \sa fontPointSize() setCurrentFont() setFontFamily()*/void QTextEdit::setFontPointSize(qreal s){ QTextCharFormat fmt; fmt.setFontPointSize(s); mergeCurrentCharFormat(fmt);}/*! Sets the font weight of the current format to \a w. \sa fontWeight() setCurrentFont() setFontFamily() QFont::Weight*/void QTextEdit::setFontWeight(int w){ QTextCharFormat fmt; fmt.setFontWeight(w); mergeCurrentCharFormat(fmt);}/*! If \a underline is true, sets the current format to underline; otherwise sets the current format to non-underline. \sa fontUnderline()*/void QTextEdit::setFontUnderline(bool underline){ QTextCharFormat fmt; fmt.setFontUnderline(underline); mergeCurrentCharFormat(fmt);}/*! If \a italic is true, sets the current format to italic; otherwise sets the current format to non-italic. \sa fontItalic()*/void QTextEdit::setFontItalic(bool italic){ QTextCharFormat fmt; fmt.setFontItalic(italic); mergeCurrentCharFormat(fmt);}/*! Sets the text color of the current format to \a c. \sa textColor()*/void QTextEdit::setTextColor(const QColor &c){ QTextCharFormat fmt; fmt.setForeground(QBrush(c)); mergeCurrentCharFormat(fmt);}/*! Sets the font of the current format to \a f. \sa currentFont() setFontPointSize() setFontFamily()*/void QTextEdit::setCurrentFont(const QFont &f){ QTextCharFormat fmt; fmt.setFont(f); mergeCurrentCharFormat(fmt);}/*! \fn void QTextEdit::undo() const Undoes the last operation. If there is no operation to undo, i.e. there is no undo step in the undo/redo history, nothing happens. \sa redo()*//*! \fn void QTextEdit::redo() const Redoes the last operation. If there is no operation to redo, i.e. there is no redo step in the undo/redo history, nothing happens. \sa undo()*/#ifndef QT_NO_CLIPBOARD/*! Copies the selected text to the clipboard and deletes it from the text edit. If there is no selected text nothing happens. \sa copy() paste()*/void QTextEdit::cut(){ Q_D(QTextEdit); if (d->readOnly || !d->cursor.hasSelection()) return; copy(); d->cursor.removeSelectedText();}/*! Copies any selected text to the clipboard. \sa copyAvailable()*/void QTextEdit::copy(){ Q_D(QTextEdit); if (!d->cursor.hasSelection()) return; QMimeData *data = createMimeDataFromSelection(); QApplication::clipboard()->setMimeData(data);}/*! Pastes the text from the clipboard into the text edit at the current cursor position. If there is no text in the clipboard nothing happens. To change the behavior of this function, i.e. to modify what QTextEdit can paste and how it is being pasted, reimplement the virtual canInsertFromMimeData() and insertFromMimeData() functions. \sa cut() copy()*/void QTextEdit::paste(){ const QMimeData *md = QApplication::clipboard()->mimeData(); if (md) insertFromMimeData(md);}#endif/*! Deletes all the text in the text edit. Note that the undo/redo history is cleared by this function. \sa cut() setPlainText() setHtml()*/void QTextEdit::clear(){ Q_D(QTextEdit); // clears and sets empty content d->setContent();}/*! Selects all text. \sa copy() cut() textCursor() */void QTextEdit::selectAll(){ Q_D(QTextEdit); d->cursor.movePosition(QTextCursor::Start); d->cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); d->selectionChanged(); d->viewport->update();#ifndef QT_NO_CLIPBOARD d->setClipboardSelection();#endif}/*! \internal*/bool QTextEdit::event(QEvent *e){ Q_D(QTextEdit); if (e->type() == QEvent::ContextMenu && static_cast<QContextMenuEvent *>(e)->reason() == QContextMenuEvent::Keyboard) { Q_D(QTextEdit); ensureCursorVisible(); const QPoint cursorPos = cursorRect().center(); QContextMenuEvent ce(QContextMenuEvent::Keyboard, cursorPos, d->viewport->mapToGlobal(cursorPos)); ce.setAccepted(e->isAccepted()); const bool result = QAbstractScrollArea::event(&ce); e->setAccepted(ce.isAccepted()); return result; } else if (e->type() == QEvent::ShortcutOverride && !d->readOnly) { QKeyEvent* ke = static_cast<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_Return: case Qt::Key_Enter: 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_I: case Qt::Key_K: case Qt::Key_N: case Qt::Key_P: case Qt::Key_T:*/ 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: case Qt::Key_Up: case Qt::Key_Down: case Qt::Key_Home: case Qt::Key_End:#if !defined(Q_WS_MAC) case Qt::Key_Insert: case Qt::Key_Delete:#endif ke->accept(); default: break; } } } return QAbstractScrollArea::event(e);}/*! \internal*/void QTextEdit::timerEvent(QTimerEvent *e){ Q_D(QTextEdit); if (e->timerId() == d->cursorBlinkTimer.timerId()) { d->cursorOn = !d->cursorOn; if (d->cursor.hasSelection()) d->cursorOn &= (style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, 0, this) != 0); d->repaintCursor();#ifndef QT_NO_DRAGANDDROP } else if (e->timerId() == d->dragStartTimer.timerId()) { d->dragStartTimer.stop(); d->startDrag();#endif } else if (e->timerId() == d->trippleClickTimer.timerId()) { d->trippleClickTimer.stop(); } else if (e->timerId() == d->autoScrollTimer.timerId()) { const QPoint globalPos = QCursor::pos(); const QPoint pos = d->viewport->mapFromGlobal(globalPos); QMouseEvent ev(QEvent::MouseMove, pos, globalPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); mouseMoveEvent(&ev); }#ifdef QT_KEYPAD_NAVIGATION else if (e->timerId() == d->deleteAllTimer.timerId()) { d->deleteAllTimer.stop(); clear(); }#endif}/*! Changes the text of the text edit to the string \a text. Any previous text is removed. \a text is interpreted as plain text. Note that the undo/redo history is cleared by this function. \sa toPlainText()*/void QTextEdit::setPlainText(const QString &text){ Q_D(QTextEdit); d->setContent(Qt::PlainText, text); d->preferRichText = false;}/*! \fn QString QTextEdit::toPlainText() const Returns the text of the text edit as plain text. \sa QTextEdit::setPlainText() *//*! \property QTextEdit::html This property provides an HTML interface to the text of the text edit. toHtml() returns the text of the text edit as html. setHtml() changes the text of the text edit. Any previous text is removed. The input text is interpreted as rich text in html format. Note that the undo/redo history is cleared by calling setHtml(). \sa {Supported HTML Subset}*/void QTextEdit::setHtml(const QString &text){ Q_D(QTextEdit); d->setContent(Qt::RichText, text); d->preferRichText = true;}/*! \reimp*/void QTextEdit::keyPressEvent(QKeyEvent *e){ Q_D(QTextEdit); if (e->modifiers() & Qt::ControlModifier) { switch( e->key() ) {#ifndef QT_NO_CLIPBOARD case Qt::Key_C: case Qt::Key_Insert: case Qt::Key_F16: // Copy key on Sun keyboards e->accept(); copy(); return;#endif // QT_NO_CLIPBOARD case Qt::Key_A: e->accept(); selectAll(); return; default: break; } } if (d->readOnly) { switch (e->key()) { case Qt::Key_Home: e->accept(); d->vbar->triggerAction(QAbstractSlider::SliderToMinimum); break; case Qt::Key_End: e->accept(); d->vbar->triggerAction(QAbstractSlider::SliderToMaximum); break; case Qt::Key_Space: e->accept(); if (e->modifiers() & Qt::ShiftModifier) d->vbar->triggerAction(QAbstractSlider::SliderPageStepSub); else d->vbar->triggerAction(QAbstractSlider::SliderPageStepAdd); default: QAbstractScrollArea::keyPressEvent(e); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -