The QTextEdit class provides a widget that is used to edit and display both plain and rich text. 更多...
继承 QAbstractScrollArea .
Inherited by QTextBrowser .
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
QTextEdit is an advanced WYSIWYG viewer/editor supporting rich text formatting using HTML-style tags. It is optimized to handle large documents and to respond quickly to user input.
QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, one newline signifies a paragraph. A document consists of zero or more paragraphs. The words in the paragraph are aligned in accordance with the paragraph's alignment. Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color.
QTextEdit can display images, lists and tables. If the text is too large to view within the text edit's viewport, scroll bars will appear. The text edit can load both plain text and HTML files (a subset of HTML 3.2 and 4).
若仅仅需要显示小段富文本使用 QLabel .
The rich text support in Qt is designed to provide a fast, portable and efficient way to add reasonable online help facilities to applications, and to provide a basis for rich text editors. If you find the HTML support insufficient for your needs you may consider the use of QtWebKit , which provides a full-featured web browser widget.
QTextEdit 中的鼠标光标形状为 Qt.IBeamCursor by default. It can be changed through the 视口 ()'s cursor 特性。
QTextEdit can display a large HTML subset, including tables and images.
设置或替代文本使用 setHtml () which deletes any existing text and replaces it with the text passed in the setHtml () 调用。若调用 setHtml () with legacy HTML, and then call toHtml (), the text that is returned may have different markup, but will render the same. The entire text can be deleted with clear ().
可以插入文字自身使用 QTextCursor class or using the convenience 函数 insertHtml (), insertPlainText (), append () 或 paste (). QTextCursor is also able to insert complex objects like tables or lists into the document, and it deals with creating selections and applying changes to selected text.
By default the text edit wraps words at whitespace to fit within the text edit widget. The setLineWrapMode () function is used to specify the kind of line wrap you want, or NoWrap if you don't want any wrapping. Call setLineWrapMode () to set a fixed pixel width FixedPixelWidth ,或 character column (e.g. 80 column) FixedColumnWidth 采用 pixels or columns specified with setLineWrapColumnOrWidth (). If you use word wrap to the widget's width WidgetWidth , you can specify whether to break on whitespace or anywhere with setWordWrapMode ().
find () function can be used to find and select a given string within the text.
If you want to limit the total number of paragraphs in a QTextEdit, as for example it is often useful in a log viewer, then you can use QTextDocument 's maximumBlockCount property for that.
When QTextEdit is used read-only the key bindings are limited to navigation, and text may only be selected with the mouse:
| Keypresses | Action |
|---|---|
| 向上 | 向上移动一行。 |
| Down | 向下移动一行。 |
| Left | 向左移动一字符。 |
| Right | 向右移动一字符。 |
| PageUp | 向上移动一 (视口) 页。 |
| PageDown | 向下移动一 (视口) 页。 |
| 首页 | 移动到文本的起始。 |
| End | 移动到文本的结尾。 |
| Alt+Wheel | Scrolls the page horizontally (the Wheel is the mouse wheel). |
| Ctrl+Wheel | 缩放文本。 |
| Ctrl+A | 选择所有文本。 |
The text edit may be able to provide some meta-information. For example, the documentTitle () function will return the text from within HTML <title> 标签。
All the information about using QTextEdit as a display widget also applies here.
设置当前字符格式的属性采用 setFontItalic (), setFontWeight (), setFontUnderline (), setFontFamily (), setFontPointSize (), setTextColor () 和 setCurrentFont (). The current paragraph's alignment is set with setAlignment ().
文本选择的处理通过 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 setTextCursor (). The selection can be copied to the clipboard with copy (),或剪切到剪贴板采用 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 保持 QTextDocument 对象,可以被检索使用 document () method. You can also set your own document object using setDocument (). QTextDocument 发射 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.
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:
bool TextEdit.canInsertFromMimeData( const QMimeData *source ) const { if (source->hasImage()) return true; else return QTextEdit.canInsertFromMimeData(source); }
We add support for image MIME types by returning true. For all other MIME types, we use the default implementation.
void TextEdit.insertFromMimeData( const QMimeData *source ) { if (source->hasImage()) { QImage image = qvariant_cast<QImage>(source->imageData()); QTextCursor cursor = this->textCursor(); QTextDocument *document = this->document(); document->addResource(QTextDocument.ImageResource, QUrl("image"), image); cursor.insertImage("image"); } }
解包图像从 QVariant held by the MIME source and insert it into the document as a resource.
为编辑而实现的键绑定列表:
| Keypresses | Action |
|---|---|
| Backspace | 删除光标左侧字符。 |
| Delete | 删除光标右侧字符。 |
| Ctrl+C | 把选中文本拷贝到剪贴板。 |
| Ctrl+Insert | 把选中文本拷贝到剪贴板。 |
| Ctrl+K | 删除到行尾。 |
| Ctrl+V | 把剪贴板文本粘贴到文本编辑中。 |
| Shift+Insert | 把剪贴板文本粘贴到文本编辑中。 |
| Ctrl+X | 删除选中文本并把它拷贝到剪贴板。 |
| Shift+Delete | 删除选中文本并把它拷贝到剪贴板。 |
| Ctrl+Z | 撤消上一操作。 |
| Ctrl+Y | 重做上一操作。 |
| Left | 左移光标一字符。 |
| Ctrl+Left | 把光标左移一单词。 |
| Right | 右移光标一字符。 |
| Ctrl+Right | 把光标右移一单词。 |
| 向上 | 上移光标一行。 |
| Down | 下移光标一行。 |
| PageUp | 把光标上移一页。 |
| PageDown | 把光标下移一页。 |
| 首页 | 把光标移到行开头。 |
| Ctrl+Home | 把光标移到文本开头。 |
| End | 把光标移到行尾。 |
| Ctrl+End | 把光标移到文本末尾。 |
| Alt+Wheel | Scrolls the page horizontally (the Wheel is the mouse wheel). |
To select (mark) text hold down the Shift key whilst pressing one of the movement keystrokes, for example, Shift+Right 将选择右侧字符,而 Shift+Ctrl+Right 将选择右侧单词,等等。
| 常量 | 值 | 描述 |
|---|---|---|
| QTextEdit.AutoNone | 0 | 不做任何自动格式化。 |
| QTextEdit.AutoBulletList | 0x00000001 | 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. |
| QTextEdit.AutoAll | 0xffffffff | Apply all automatic formatting. Currently only automatic bullet lists are supported. |
AutoFormatting 类型是 typedef 对于 QFlags <AutoFormattingFlag>. It stores an OR combination of AutoFormattingFlag values.
| 常量 | 值 |
|---|---|
| QTextEdit.NoWrap | 0 |
| QTextEdit.WidgetWidth | 1 |
| QTextEdit.FixedPixelWidth | 2 |
| QTextEdit.FixedColumnWidth | 3 |
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造空 QTextEdit with parent parent .
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
构造 QTextEdit 采用父级 parent 。文本编辑将显示文本 text 。 text is interpreted as html.
返回当前段落的对齐方式。
另请参阅 setAlignment ().
返回锚点引用在位置 pos ,或 an empty string if no anchor exists at that point.
追加新段落采用 text to the end of the text edit.
注意: The new paragraph appended will have the same character format and block format as the current paragraph, determined by the position of the cursor.
另请参阅 currentCharFormat () 和 QTextCursor.blockFormat ().
This function returns true if the contents of the MIME data object, specified by source , can be decoded and inserted into the document. It is called for example when during a drag operation the mouse enters this widget and it is necessary to determine whether it is possible to accept the drag and drop operation.
Reimplement this function to enable drag and drop support for additional MIME types.
Returns whether text can be pasted from the clipboard into the textedit.
该函数在 Qt 4.2 引入。
重实现自 QWidget.changeEvent ().
This method is also a Qt slot with the C++ signature void clear() .
删除文本编辑中的所有文本。
Note that the undo/redo history is cleared by this function.
另请参阅 cut (), setPlainText (),和 setHtml ().
重实现自 QWidget.contextMenuEvent ().
展示标准上下文菜单,创建采用 createStandardContextMenu ().
If you do not want the text edit to have a context menu, you can set its contextMenuPolicy to Qt.NoContextMenu 。若 you want to customize the context menu, reimplement this function. If you want to extend the standard context menu, reimplement this function, call createStandardContextMenu () and extend the menu returned.
Information about the event is passed in the event 对象。
void MyTextEdit.contextMenuEvent(QContextMenuEvent *event) { QMenu *menu = createStandardContextMenu(); menu->addAction(tr("My Menu Item")); //... menu->exec(event->globalPos()); delete menu; }
This method is also a Qt slot with the C++ signature void copy() .
把任何选中文本拷贝到剪贴板。
另请参阅 copyAvailable ().
This function returns a new MIME data object to represent the contents of the text edit's current selection. It is called when the selection needs to be encapsulated into a new QMimeData object; for example, when a drag and drop operation is started, or when data is copyied to the clipboard.
If you reimplement this function, note that the ownership of the returned QMimeData 对象被传递给 the caller. The selection can be retrieved by using the textCursor () 函数。
This function creates the standard context menu which is shown when the user clicks on the text edit with the right mouse button. It is called from the default contextMenuEvent () handler. The popup menu's ownership is transferred to the caller.
推荐使用 createStandardContextMenu( QPoint ) version instead which will enable the actions that are sensitive to where the user clicked.
This function creates the standard context menu which is shown when the user clicks on the text edit with the right mouse button. It is called from the default contextMenuEvent () handler and it takes the position of where the mouse click was. This can enable actions that are sensitive to the position where the user clicked. The popup menu's ownership is transferred to the caller.
该函数在 Qt 4.4 引入。
Returns the char format that is used when inserting new text.
另请参阅 setCurrentCharFormat ().
返回当前格式的字体。
另请参阅 setCurrentFont (), setFontFamily (),和 setFontPointSize ().
返回 QTextCursor 在位置 pos (在视口坐标中)。
返回矩形 (在视口坐标中) 包括 cursor .
返回矩形 (在视口坐标中) 包括 cursor of the text edit.
This method is also a Qt slot with the C++ signature void cut() .
Copies the selected text to the clipboard and deletes it from the text edit.
若没有选中文本,什么都不会发生。
返回指向底层文档的指针。
另请参阅 setDocument ().
重实现自 QWidget.dragEnterEvent ().
重实现自 QWidget.dragLeaveEvent ().
重实现自 QWidget.dragMoveEvent ().
重实现自 QWidget.dropEvent ().
Ensures that the cursor is visible by scrolling the text edit if necessary.
返回先前设置的额外选定。
该函数在 Qt 4.2 引入。
另请参阅 setExtraSelections ().
查找字符串的下一出现 exp ,使用 given options . Returns true if exp was found and changes the cursor to select the match; otherwise returns false.
重实现自 QWidget.focusInEvent ().
重实现自 QWidget.focusNextPrevChild ().
重实现自 QWidget.focusOutEvent ().
返回当前格式的字体系列。
另请参阅 setFontFamily (), setCurrentFont (),和 setFontPointSize ().
Returns true if the font of the current format is italic; otherwise returns false.
另请参阅 setFontItalic ().
返回当前格式字体的点尺寸。
另请参阅 setFontFamily (), setCurrentFont (),和 setFontPointSize ().
Returns true if the font of the current format is underlined; otherwise returns false.
另请参阅 setFontUnderline ().
返回当前格式的字体权重。
另请参阅 setFontWeight (), setCurrentFont (), setFontPointSize (),和 QFont.Weight .
重实现自 QWidget.inputMethodEvent ().
重实现自 QWidget.inputMethodQuery ().
This function inserts the contents of the MIME data object, 指定通过 source , into the text edit at the current cursor position. It is called whenever text is inserted as the result of a clipboard paste operation, or when the text edit accepts data from a drag and drop operation.
Reimplement this function to enable drag and drop support for additional MIME types.
This method is also a Qt slot with the C++ signature void insertHtml(const QString&) .
方便槽,插入 text which is assumed to be of html formatting at the current cursor position.
相当于:
edit->textCursor().insertHtml(fragment);
注意: When using this function with a style sheet, the style sheet will only apply to the current block in the document. In order to apply a style sheet throughout a document, use QTextDocument.setDefaultStyleSheet () 代替。
This method is also a Qt slot with the C++ signature void insertPlainText(const QString&) .
方便槽,插入 text 在当前光标 position.
它相当于
edit->textCursor().insertText(text);
重实现自 QWidget.keyPressEvent ().
重实现自 QWidget.keyReleaseEvent ().
加载指定资源通过给定 type and name .
This function is an extension of QTextDocument.loadResource ().
另请参阅 QTextDocument.loadResource ().
Merges the properties specified in modifier into the current character format by calling QTextCursor.mergeCharFormat on the editor's cursor. If the editor has a selection then the properties of modifier are directly applied to the selection.
另请参阅 QTextCursor.mergeCharFormat ().
重实现自 QWidget.mouseDoubleClickEvent ().
重实现自 QWidget.mouseMoveEvent ().
重实现自 QWidget.mousePressEvent ().
重实现自 QWidget.mouseReleaseEvent ().
移动光标通过履行给定 operation .
若 mode is QTextCursor.KeepAnchor , cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.
该函数在 Qt 4.2 引入。
另请参阅 QTextCursor.movePosition ().
重实现自 QWidget.paintEvent ().
This event handler can be reimplemented in a subclass to receive paint events passed in event . It is usually unnecessary to reimplement this function in a subclass of QTextEdit .
警告: The underlying text document must not be modified from within a reimplementation of this function.
This method is also a Qt slot with the C++ signature void paste() .
Pastes the text from the clipboard into the text edit at the current cursor position.
若剪贴板中没有文本,什么都不发生。
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 () 函数。
Convenience function to print the text edit's document to the given printer . This is equivalent to calling the print method on the document directly except that this function also supports QPrinter.Selection as print range.
该函数在 Qt 4.3 引入。
另请参阅 QTextDocument.print ().
This method is also a Qt slot with the C++ signature void redo() .
重做上一操作。
If there is no operation to redo, i.e. there is no redo step in the undo/redo history, nothing happens.
该函数在 Qt 4.2 引入。
另请参阅 undo ().
重实现自 QWidget.resizeEvent ().
重实现自 QAbstractScrollArea.scrollContentsBy ().
This method is also a Qt slot with the C++ signature void scrollToAnchor(const QString&) .
Scrolls the text edit so that the anchor with the given name is visible; does nothing if the name is empty, or is already visible, or isn't found.
This method is also a Qt slot with the C++ signature void selectAll() .
选择所有文本。
另请参阅 copy (), cut (),和 textCursor ().
This method is also a Qt slot with the C++ signature void setAlignment(Qt::Alignment) .
把当前段落的对齐方式设为 a . Valid alignments are Qt.AlignLeft , Qt.AlignRight , Qt.AlignJustify and Qt.AlignCenter (which centers horizontally).
另请参阅 alignment ().
把新文本插入时使用的字符格式设为 format 通过调用 QTextCursor.setCharFormat () on the editor's cursor. If the editor has a selection then the char format is directly applied to the selection.
另请参阅 currentCharFormat ().
This method is also a Qt slot with the C++ signature void setCurrentFont(const QFont&) .
把当前格式的字体设为 f .
另请参阅 currentFont (), setFontPointSize (),和 setFontFamily ().
Makes document the new document of the text editor.
注意: 编辑器 does not take ownership of the document unless it is the document's parent object. The parent object of the provided document remains the owner of the 对象。
The editor does not delete the current document, even if it is a child of the editor.
另请参阅 document ().
This function allows temporarily marking certain regions in the document with a given color, specified as selections . This can be useful for example in a programming editor to mark a whole line of text with a given background color to indicate the existence of a breakpoint.
该函数在 Qt 4.2 引入。
另请参阅 QTextEdit.ExtraSelection and extraSelections ().
This method is also a Qt slot with the C++ signature void setFontFamily(const QString&) .
把当前格式的字体系列设为 fontFamily .
另请参阅 fontFamily () 和 setCurrentFont ().
This method is also a Qt slot with the C++ signature void setFontItalic(bool) .
若 italic is true, sets the current format to italic; otherwise sets the current format to non-italic.
另请参阅 fontItalic ().
This method is also a Qt slot with the C++ signature void setFontPointSize(qreal) .
将当前格式的点尺寸设为 s .
注意:若 s is zero or negative, the behavior of this function is not defined.
另请参阅 fontPointSize (), setCurrentFont (),和 setFontFamily ().
This method is also a Qt slot with the C++ signature void setFontUnderline(bool) .
若 underline is true, sets the current format to underline; otherwise sets the current format to non-underline.
另请参阅 fontUnderline ().
This method is also a Qt slot with the C++ signature void setFontWeight(int) .
Sets the font weight of the current format to the given weight , where the value used is in the range defined by the QFont.Weight 枚举。
另请参阅 fontWeight (), setCurrentFont (),和 setFontFamily ().
This method is also a Qt slot with the C++ signature void setHtml(const QString&) .
This method is also a Qt slot with the C++ signature void setPlainText(const QString&) .
This method is also a Qt slot with the C++ signature void setText(const QString&) .
设置文本编辑的 text . The text can be plain text or HTML and the text edit will try to guess the right format.
使用 setHtml () 或 setPlainText () directly to avoid text edit's guessing.
该函数在 Qt 4.2 引入。
另请参阅 text (), toPlainText (),和 toHtml ().
This method is also a Qt slot with the C++ signature void setTextBackgroundColor(const QColor&) .
把当前格式的文本背景颜色设为 c .
该函数在 Qt 4.4 引入。
另请参阅 textBackgroundColor ().
This method is also a Qt slot with the C++ signature void setTextColor(const QColor&) .
把当前格式的文本颜色设为 c .
另请参阅 textColor ().
设置可见 cursor .
另请参阅 textCursor ().
重实现自 QWidget.showEvent ().
返回当前格式的文本背景颜色。
该函数在 Qt 4.4 引入。
另请参阅 setTextBackgroundColor ().
返回当前格式的文本颜色。
另请参阅 setTextColor ().
返回副本为 QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QTextEdit 的光标; 使用 setTextCursor () to update the visible cursor.
另请参阅 setTextCursor ().
This method is also a Qt slot with the C++ signature void undo() .
撤消上一操作。
If there is no operation to undo, i.e. there is no undo step in the undo/redo history, nothing happens.
该函数在 Qt 4.2 引入。
另请参阅 redo ().
重实现自 QWidget.wheelEvent ().
This method is also a Qt slot with the C++ signature void zoomIn(int = 1) .
Zooms in on the text by making the base font size range points larger and recalculating all font sizes to be the new size. This does not change the size of any images.
另请参阅 zoomOut ().
This method is also a Qt slot with the C++ signature void zoomOut(int = 1) .
这是重载函数。
Zooms out on the text by making the base font size range points smaller and recalculating all font sizes to be the new size. This does not change the size of any images.
另请参阅 zoomIn ().
This is the default overload of this signal.
This signal is emitted when text is selected or de-selected in the text edit.
当有文本被选中时,此信号会被发射采用 yes set to true. If no text has been selected or if the selected text is de-selected this signal is emitted with yes 设为 false。
若 yes 为 true 则 copy () can be used to copy the selection to the clipboard. If yes 为 false 则 copy () 什么都不做。
另请参阅 selectionChanged ().
This is the default overload of this signal.
This signal is emitted if the current character format has changed, for example caused by a change of the cursor position.
新的格式为 f .
另请参阅 setCurrentCharFormat ().
This is the default overload of this signal.
This signal is emitted whenever the position of the cursor 改变。
This is the default overload of this signal.
This signal is emitted whenever redo operations become available ( available is true) or unavailable ( available is false).
This is the default overload of this signal.
此信号被发射每当选定改变。
另请参阅 copyAvailable ().
This is the default overload of this signal.
This signal is emitted whenever the document's content changes; for example, when text is inserted or deleted, or when formatting is applied.
This is the default overload of this signal.
This signal is emitted whenever undo operations become available ( available is true) or unavailable ( available is false).