📄 qtextedit.cpp
字号:
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);}/*! \since 4.2 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()*/void QTextEdit::undo(){ Q_D(QTextEdit); d->control->undo();}void QTextEdit::redo(){ Q_D(QTextEdit); d->control->redo();}/*! \fn void QTextEdit::undo() const \fn void QTextEdit::redo() const \overload Use the non-const overload instead.*//*! \fn void QTextEdit::redo() \since 4.2 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); d->control->cut();}/*! Copies any selected text to the clipboard. \sa copyAvailable()*/void QTextEdit::copy(){ Q_D(QTextEdit); d->control->copy();}/*! 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(){ Q_D(QTextEdit); d->control->paste();}#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->control->clear();}/*! Selects all text. \sa copy() cut() textCursor() */void QTextEdit::selectAll(){ Q_D(QTextEdit); d->control->selectAll();}/*! \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 || e->type() == QEvent::ToolTip) { d->sendControlEvent(e); }#ifdef QT_KEYPAD_NAVIGATION else if (e->type() == QEvent::EnterEditFocus || e->type() == QEvent::LeaveEditFocus) { if (QApplication::keypadNavigationEnabled()) d->sendControlEvent(e); }#endif return QAbstractScrollArea::event(e);}/*! \internal*/void QTextEdit::timerEvent(QTimerEvent *e){ Q_D(QTextEdit); 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->control->setPlainText(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 and the undo/redo history is cleared. The input text is interpreted as rich text in html format. \note It is the responsibility of the caller to make sure that the text is correctly decoded when a QString containing HTML is created and passed to setHtml(). \sa {Supported HTML Subset}, plainText*/void QTextEdit::setHtml(const QString &text){ Q_D(QTextEdit); d->control->setHtml(text); d->preferRichText = true;}/*! \reimp*/void QTextEdit::keyPressEvent(QKeyEvent *e){ Q_D(QTextEdit);#ifdef QT_KEYPAD_NAVIGATION switch (e->key()) { case Qt::Key_Select: if (QApplication::keypadNavigationEnabled()) { // code assumes linksaccessible + editable isn't meaningful if (d->control->textInteractionFlags() & Qt::TextEditable) { setEditFocus(!hasEditFocus()); } else { if (!hasEditFocus()) setEditFocus(true); else { QTextCursor cursor = d->control->textCursor(); QTextCharFormat charFmt = cursor.charFormat(); if (!(d->control->textInteractionFlags() & Qt::LinksAccessibleByKeyboard) || !cursor.hasSelection() || charFmt.anchorHref().isEmpty()) { e->accept(); return; } } } } break; case Qt::Key_Back: case Qt::Key_No: if (!QApplication::keypadNavigationEnabled() || (QApplication::keypadNavigationEnabled() && !hasEditFocus())) { e->ignore(); return; } break; default: if (QApplication::keypadNavigationEnabled()) { if (!hasEditFocus() && !(e->modifiers() & Qt::ControlModifier)) { if (e->text()[0].isPrint()) { setEditFocus(true); clear(); } else { e->ignore(); return; } } } break; }#endif if (!(d->control->textInteractionFlags() & Qt::TextEditable)) { switch (e->key()) { case Qt::Key_Space: e->accept(); if (e->modifiers() & Qt::ShiftModifier) d->vbar->triggerAction(QAbstractSlider::SliderPageStepSub); else d->vbar->triggerAction(QAbstractSlider::SliderPageStepAdd); break; default: d->sendControlEvent(e); if (!e->isAccepted() && e->modifiers() == Qt::NoModifier) { if (e->key() == Qt::Key_Home) { d->vbar->triggerAction(QAbstractSlider::SliderToMinimum); e->accept(); } else if (e->key() == Qt::Key_End) { d->vbar->triggerAction(QAbstractSlider::SliderToMaximum); e->accept(); } } if (!e->isAccepted()) { QAbstractScrollArea::keyPressEvent(e); } } return; }#ifndef QT_NO_SHORTCUT if (e == QKeySequence::MoveToPreviousPage) { e->accept(); d->pageUpDown(QTextCursor::Up, QTextCursor::MoveAnchor); return; } else if (e == QKeySequence::MoveToNextPage) { e->accept(); d->pageUpDown(QTextCursor::Down, QTextCursor::MoveAnchor); return; } else if (e == QKeySequence::SelectPreviousPage) { e->accept(); d->pageUpDown(QTextCursor::Up, QTextCursor::KeepAnchor); return; } else if (e ==QKeySequence::SelectNextPage) { e->accept(); d->pageUpDown(QTextCursor::Down, QTextCursor::KeepAnchor); return; }#endif // QT_NO_SHORTCUT { QTextCursor cursor = d->control->textCursor(); const QString text = e->text(); if (cursor.atBlockStart() && (d->autoFormatting & AutoBulletList) && (text.length() == 1) && (text.at(0) == QLatin1Char('-') || text.at(0) == QLatin1Char('*')) && (!cursor.currentList())) { d->createAutoBulletList(); e->accept(); return; } } d->sendControlEvent(e);#ifdef QT_KEYPAD_NAVIGATION if (!e->isAccepted()) { switch (e->key()) { case Qt::Key_Up: case Qt::Key_Down: if (QApplication::keypadNavigationEnabled()) { // Cursor position didn't change, so we want to leave // these keys to change focus. e->ignore(); return; } break; case Qt::Key_Back: if (!e->isAutoRepeat()) { if (QApplication::keypadNavigationEnabled()) { if (document()->isEmpty() || !(d->control->textInteractionFlags() & Qt::TextEditable)) { setEditFocus(false); e->accept(); } else if (!d->deleteAllTimer.isActive()) { e->accept(); d->deleteAllTimer.start(750, this); } } else { e->ignore(); return; } } break; default: break; } }#endif}/*! \reimp*/void QTextEdit::keyReleaseEvent(QKeyEvent *e){#ifdef QT_KEYPAD_NAVIGATION Q_D(QTextEdit); if (QApplication::keypadNavigationEnabled()) { if (!e->isAutoRepeat() && e->key() == Qt::Key_Back && d->deleteAllTimer.isActive()) { d->deleteAllTimer.stop(); QTextCursor cursor = d->control->textCursor(); QTextBlockFormat blockFmt = cursor.blockFormat(); QTextList *list = cursor.currentList(); if (list && cursor.atBlockStart()) { list->remove(cursor.block()); } else if (cursor.atBlockStart() && blockFmt.indent() > 0) { blockFmt.setIndent(blockFmt.indent() - 1); cursor.setBlockFormat(blockFmt); } else { cursor.deletePreviousChar(); } setTextCursor(cursor); } }#else Q_UNUSED(e);#endif}/*! Loads the resource specified by the given \a type and \a name. This function is an extension of QTextDocument::loadResource(). \sa QTextDocument::loadResource()*/QVariant QTextEdit::loadResource(int type, const QUrl &name){ Q_UNUSED(type); Q_UNUSED(name); return QVariant();}/*! \reimp*/void QTextEdit::resizeEvent(QResizeEvent *e){ Q_D(QTextEdit); if (d->lineWrap == NoWrap) { QTextDocument *doc = d->control->document(); QVariant alignmentProperty = doc->documentLayout()->property("contentHasAlignment"); if (!doc->pageSize().isNull() && alignmentProperty.type() == QVariant::Bool && !alignmentProperty.toBool()) { d->_q_adjustScrollbars(); return; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -