📄 qtextedit.cpp
字号:
\section1 Using QTextEdit as an Editor All the information about using QTextEdit as a display widget also applies here. The current char format's attributes are set with setFontItalic(), setFontWeight(), setFontUnderline(), setFontFamily(), setFontPointSize(), setTextColor() and setCurrentFont(). The current paragraph's alignment is set with setAlignment(). Selection of text is handled by the QTextCursor class, which provides functionality for creating selections, retrieving the text contents or deleting selections. You can retrieve the object that corresponds with the user-visible cursor using the textCursor() method. If you want to set a selection in QTextEdit just create one on a QTextCursor object and then make that cursor the visible cursor using setCursor(). The selection can be copied to the clipboard with copy(), or cut to the clipboard with cut(). The entire text can be selected using selectAll(). When the cursor is moved and the underlying formatting attributes change, the currentCharFormatChanged() signal is emitted to reflect the new attributes at the new cursor position. QTextEdit holds a QTextDocument object which can be retrieved using the document() method. You can also set your own document object using setDocument(). QTextDocument emits a textChanged() signal if the text changes and it also provides a isModified() function which will return true if the text has been modified since it was either loaded or since the last call to setModified with false as argument. In addition it provides methods for undo and redo. \section2 Drag and Drop QTextEdit also supports custom drag and drop behavior. By default, QTextEdit will insert plain text, HTML and rich text when the user drops data of these MIME types onto a document. Reimplement canInsertFromMimeData() and insertFromMimeData() to add support for additional MIME types. For example, to allow the user to drag and drop an image onto a QTextEdit, you could the implement these functions in the following way: \quotefromfile snippets/textdocument-imagedrop/textedit.cpp \skipto bool TextEdit::canInsertFromMimeData \printuntil /^\}/ We add support for image MIME types by returning true. For all other MIME types, we use the default implementation. \skipto void TextEdit::insertFromMimeData \printuntil /^\}/ We unpack the image from the QVariant held by the MIME source and insert it into the document as a resource. \section2 Editing Key Bindings The list of key bindings which are implemented for editing: \table \header \i Keypresses \i Action \row \i Backspace \i Deletes the character to the left of the cursor. \row \i Delete \i Deletes the character to the right of the cursor. \row \i Ctrl+C \i Copy the selected text to the clipboard. \row \i Ctrl+Insert \i Copy the selected text to the clipboard. \row \i Ctrl+K \i Deletes to the end of the line. \row \i Ctrl+V \i Pastes the clipboard text into text edit. \row \i Shift+Insert \i Pastes the clipboard text into text edit. \row \i Ctrl+X \i Deletes the selected text and copies it to the clipboard. \row \i Shift+Delete \i Deletes the selected text and copies it to the clipboard. \row \i Ctrl+Z \i Undoes the last operation. \row \i Ctrl+Y \i Redoes the last operation. \row \i LeftArrow \i Moves the cursor one character to the left. \row \i Ctrl+LeftArrow \i Moves the cursor one word to the left. \row \i RightArrow \i Moves the cursor one character to the right. \row \i Ctrl+RightArrow \i Moves the cursor one word to the right. \row \i UpArrow \i Moves the cursor one line up. \row \i Ctrl+UpArrow \i Moves the cursor one word up. \row \i DownArrow \i Moves the cursor one line down. \row \i Ctrl+Down Arrow \i Moves the cursor one word down. \row \i PageUp \i Moves the cursor one page up. \row \i PageDown \i Moves the cursor one page down. \row \i Home \i Moves the cursor to the beginning of the line. \row \i Ctrl+Home \i Moves the cursor to the beginning of the text. \row \i End \i Moves the cursor to the end of the line. \row \i Ctrl+End \i Moves the cursor to the end of the text. \row \i Alt+Wheel \i Scrolls the page horizontally (the Wheel is the mouse wheel). \endtable To select (mark) text hold down the Shift key whilst pressing one of the movement keystrokes, for example, \e{Shift+Right Arrow} will select the character to the right, and \e{Shift+Ctrl+Right Arrow} will select the word to the right, etc. \sa QTextDocument, QTextCursor, {Application Example}, {Syntax Highlighter Example}, {Rich Text Processing}*//*! \property QTextEdit::plainText \since 4.3 This property gets and sets the text edit's contents as plain text. Previous contents are removed and undo/redo history is reset when the property is set. If the text edit has another content type, it will not be replaced by plain text when you call toPlainText(). \sa html*//*! \property QTextEdit::undoRedoEnabled \brief whether undo and redo are enabled Users are only able to undo or redo actions if this property is true, and if there is an action that can be undone (or redone).*//*! \enum QTextEdit::LineWrapMode \value NoWrap \value WidgetWidth \value FixedPixelWidth \value FixedColumnWidth*//*! \enum QTextEdit::AutoFormattingFlag \value AutoNone Don't do any automatic formatting. \value AutoBulletList Automatically create bullet lists (e.g. when the user enters an asterisk ('*') in the left most column, or presses Enter in an existing list item. \value AutoAll Apply all automatic formatting. Currently only automatic bullet lists are supported.*/#ifdef QT3_SUPPORT/*! \enum QTextEdit::CursorAction \value MoveBackward \value MoveForward \value MoveWordBackward \value MoveWordForward \value MoveUp \value MoveDown \value MoveLineStart \value MoveLineEnd \value MoveHome \value MoveEnd \value MovePageUp \value MovePageDown \omitvalue MovePgUp \omitvalue MovePgDown*/#endif/*! Constructs an empty QTextEdit with parent \a parent.*/QTextEdit::QTextEdit(QWidget *parent) : QAbstractScrollArea(*new QTextEditPrivate, parent){ Q_D(QTextEdit); d->init();}/*! \internal*/QTextEdit::QTextEdit(QTextEditPrivate &dd, QWidget *parent) : QAbstractScrollArea(dd, parent){ Q_D(QTextEdit); d->init();}/*! Constructs a QTextEdit with parent \a parent. The text edit will display the text \a text. The text is interpreted as html.*/QTextEdit::QTextEdit(const QString &text, QWidget *parent) : QAbstractScrollArea(*new QTextEditPrivate, parent){ Q_D(QTextEdit); d->init(text);}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QTextEdit::QTextEdit(QWidget *parent, const char *name) : QAbstractScrollArea(*new QTextEditPrivate, parent){ Q_D(QTextEdit); d->init(); setObjectName(QString::fromAscii(name));}#endif/*! Destructor.*/QTextEdit::~QTextEdit(){}/*! Returns the point size of the font of the current format. \sa setFontFamily() setCurrentFont() setFontPointSize()*/qreal QTextEdit::fontPointSize() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().fontPointSize();}/*! Returns the font family of the current format. \sa setFontFamily() setCurrentFont() setFontPointSize()*/QString QTextEdit::fontFamily() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().fontFamily();}/*! Returns the font weight of the current format. \sa setFontWeight() setCurrentFont() setFontPointSize() QFont::Weight*/int QTextEdit::fontWeight() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().fontWeight();}/*! Returns true if the font of the current format is underlined; otherwise returns false. \sa setFontUnderline()*/bool QTextEdit::fontUnderline() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().fontUnderline();}/*! Returns true if the font of the current format is italic; otherwise returns false. \sa setFontItalic()*/bool QTextEdit::fontItalic() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().fontItalic();}/*! Returns the text color of the current format. \sa setTextColor()*/QColor QTextEdit::textColor() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().foreground().color();}/*! Returns the font of the current format. \sa setCurrentFont() setFontFamily() setFontPointSize()*/QFont QTextEdit::currentFont() const{ Q_D(const QTextEdit); return d->control->textCursor().charFormat().font();}/*! Sets the alignment of the current paragraph to \a a. Valid alignments are Qt::AlignLeft, Qt::AlignRight, Qt::AlignJustify and Qt::AlignCenter (which centers horizontally).*/void QTextEdit::setAlignment(Qt::Alignment a){ Q_D(QTextEdit); QTextBlockFormat fmt; fmt.setAlignment(a); QTextCursor cursor = d->control->textCursor(); cursor.mergeBlockFormat(fmt); d->control->setTextCursor(cursor);}/*! Returns the alignment of the current paragraph. \sa setAlignment()*/Qt::Alignment QTextEdit::alignment() const{ Q_D(const QTextEdit); return d->control->textCursor().blockFormat().alignment();}/*! Makes \a document the new document of the text editor. The parent QObject of the provided document remains the owner of the object. If the current document is a child of the text editor, then it is deleted. \sa document()*/void QTextEdit::setDocument(QTextDocument *document){ Q_D(QTextEdit); d->control->setDocument(document); d->updateDefaultTextOption(); d->relayoutDocument();}/*! Returns a pointer to the underlying document. \sa setDocument()*/QTextDocument *QTextEdit::document() const{ Q_D(const QTextEdit); return d->control->document();}/*! Sets the visible \a cursor.*/void QTextEdit::setTextCursor(const QTextCursor &cursor){ Q_D(QTextEdit); d->control->setTextCursor(cursor);}/*! 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->control->textCursor();}/*! 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);}/*! \fn void QTextEdit::setFontWeight(int weight) Sets the font weight of the current format to the given \a weight, where the value used is in the range defined by the QFont::Weight enum. \sa fontWeight(), setCurrentFont(), setFontFamily()*/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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -