QPlainTextEdit Class Reference

[ QtGui module]

The QPlainTextEdit class provides a widget that is used to edit and display plain text. 更多...

继承 QAbstractScrollArea .

类型

方法

Qt Signals


详细描述

The QPlainTextEdit class provides a widget that is used to edit and display plain text.

介绍和概念

QPlainTextEdit is an advanced viewer/editor supporting plain text. It is optimized to handle large documents and to respond quickly to user input.

QPlainText 使用了一样的技术和概念,如 QTextEdit , but is optimized for plain text handling.

QPlainTextEdit 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. Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color.

QPlainTextEdit 中的鼠标光标形状为 Qt.IBeamCursor by default. It can be changed through the 视口 ()'s cursor 特性。

使用 QPlainTextEdit 作为显示 Widget

设置或替代文本使用 setPlainText () which deletes the existing text and replaces it with the text passed to setPlainText ().

插入文本可用使用 QTextCursor class or using the convenience 函数 insertPlainText (), appendPlainText () or paste ().

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, WidgetWidth or NoWrap if you don't want any wrapping. 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 QPlainTextEdit, as it is for example useful in a log viewer, then you can use the maximumBlockCount 特性。组合 setMaximumBlockCount () and appendPlainText () turns QPlainTextEdit into an efficient viewer for log text. The scrolling can be reduced with the centerOnScroll () property, making the log viewer even faster. Text can be formatted in a limited way, either using a syntax highlighter (see below), or by appending html-formatted text with appendHtml (). While QPlainTextEdit does not support complex rich text rendering with tables and floats, it does support limited paragraph-based formatting that you may need in a log viewer.

只读键绑定

When QPlainTextEdit is used read-only the key bindings are limited to navigation, and text may only be selected with the mouse:

Keypresses Action
Qt.UpArrow 向上移动一行。
Qt.DownArrow 向下移动一行。
Qt.LeftArrow 向左移动一字符。
Qt.RightArrow 向右移动一字符。
PageUp 向上移动一 (视口) 页。
PageDown 向下移动一 (视口) 页。
首页 移动到文本的起始。
End 移动到文本的结尾。
Alt+Wheel Scrolls the page horizontally (the Wheel is the mouse wheel).
Ctrl+Wheel 缩放文本。
Ctrl+A 选择所有文本。

使用 QPlainTextEdit 作为编辑器

All the information about using QPlainTextEdit as a display widget also applies here.

文本选择的处理通过 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 QPlainTextEdit 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 (),或剪切到剪贴板采用 cut (). The entire text can be selected using selectAll ().

QPlainTextEdit 保持 QTextDocument object which can be retrieved using the 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 , QPlainTextEdit works together with QSyntaxHighlighter .

编辑键绑定

为编辑而实现的键绑定列表:

Keypresses Action
Backspace 删除光标左侧字符。
Delete 删除光标右侧字符。
Ctrl+C 把选中文本拷贝到剪贴板。
Ctrl+Insert 把选中文本拷贝到剪贴板。
Ctrl+K 删除到行尾。
Ctrl+V 把剪贴板文本粘贴到文本编辑中。
Shift+Insert 把剪贴板文本粘贴到文本编辑中。
Ctrl+X 删除选中文本并把它拷贝到剪贴板。
Shift+Delete 删除选中文本并把它拷贝到剪贴板。
Ctrl+Z 撤消上一操作。
Ctrl+Y 重做上一操作。
LeftArrow 左移光标一字符。
Ctrl+LeftArrow 把光标左移一单词。
RightArrow 右移光标一字符。
Ctrl+RightArrow 把光标右移一单词。
UpArrow 上移光标一行。
Ctrl+UpArrow 把光标上移一单词。
DownArrow 下移光标一行。
Ctrl+Down Arrow 把光标下移一单词。
PageUp 把光标上移一页。
PageDown 把光标下移一页。
首页 把光标移到行开头。
Ctrl+Home 把光标移到文本开头。
End 把光标移到行尾。
Ctrl+End 把光标移到文本末尾。
Alt+Wheel Scrolls the page horizontally (the Wheel is the mouse wheel).
Ctrl+Wheel 缩放文本。

To select (mark) text hold down the Shift key whilst pressing one of the movement keystrokes, for example, Shift+Right Arrow 将选择右侧字符,而 Shift+Ctrl+Right Arrow will select the word to the right, etc.

和 QTextEdit 的差异

QPlainTextEdit is a thin class, implemented by using most of the technology that is behind QTextEdit and QTextDocument . Its performance benefits over QTextEdit stem mostly from using a different and simplified text layout called QPlainTextDocumentLayout 在 text document (see QTextDocument.setDocumentLayout ()). The plain text document layout does not support tables nor embedded frames, and replaces a pixel-exact height calculation with a line-by-line respectively paragraph-by-paragraph scrolling approach . This makes it possible to handle significantly larger documents, and still resize the editor with line wrap enabled in real time. It also makes for a fast log viewer (see setMaximumBlockCount ()).


类型文档编制

QPlainTextEdit.LineWrapMode

常量
QPlainTextEdit.NoWrap 0
QPlainTextEdit.WidgetWidth 1

方法文档编制

QPlainTextEdit.__init__ ( self , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造空 QPlainTextEdit 采用父级 parent .

QPlainTextEdit.__init__ ( self , QString  text , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

构造 QPlainTextEdit 采用父级 parent . The text edit will display the plain text text .

QString QPlainTextEdit.anchorAt ( self , QPoint   pos )

返回锚点引用在位置 pos ,或 an empty string if no anchor exists at that point.

该函数在 Qt 4.7 引入。

QPlainTextEdit.appendHtml ( self , QString  html )

This method is also a Qt slot with the C++ signature void appendHtml(const QString&) .

追加新段落采用 html to the end of the text edit.

appendPlainText ()

QPlainTextEdit.appendPlainText ( self , QString  text )

This method is also a Qt slot with the C++ signature void appendPlainText(const QString&) .

追加新段落采用 text to the end of the text edit.

另请参阅 appendHtml ().

bool QPlainTextEdit.backgroundVisible ( self )

QRectF QPlainTextEdit.blockBoundingGeometry ( self , QTextBlock   block )

Returns the bounding rectangle of the text block in content coordinates. Translate the rectangle with the contentOffset () to get visual coordinates on the viewport.

另请参阅 firstVisibleBlock () 和 blockBoundingRect ().

QRectF QPlainTextEdit.blockBoundingRect ( self , QTextBlock   block )

Returns the bounding rectangle of the text block 在 block's own coordinates.

另请参阅 blockBoundingGeometry ().

int QPlainTextEdit.blockCount ( self )

bool QPlainTextEdit.canInsertFromMimeData ( self , QMimeData   source )

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.

bool QPlainTextEdit.canPaste ( self )

Returns whether text can be pasted from the clipboard into the textedit.

QPlainTextEdit.centerCursor ( self )

This method is also a Qt slot with the C++ signature void centerCursor() .

Scrolls the document in order to center the cursor vertically.

另请参阅 ensureCursorVisible () and centerOnScroll .

bool QPlainTextEdit.centerOnScroll ( self )

QPlainTextEdit.changeEvent ( self , QEvent   e )

重实现自 QWidget.changeEvent ().

QPlainTextEdit.clear ( self )

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 ().

QPointF QPlainTextEdit.contentOffset ( self )

返回视口坐标中的内容原点。

The origin of the content of a plain text edit is always the top left corner of the first visible text block. The content offset is different from (0,0) when the text has been scrolled horizontally, or when the first visible block has been scrolled partially off the screen, i.e. the visible text does not start with the first line of the first visible block, or when the first visible block is the very first block and the editor displays a margin.

另请参阅 firstVisibleBlock (), horizontalScrollBar (), and verticalScrollBar ().

QPlainTextEdit.contextMenuEvent ( self , QContextMenuEvent   e )

重实现自 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 MyQPlainTextEdit.contextMenuEvent(QContextMenuEvent *event)
 {
     QMenu *menu = createStandardContextMenu();
     menu->addAction(tr("My Menu Item"));
     //...
     menu->exec(event->globalPos());
     delete menu;
 }
			

QPlainTextEdit.copy ( self )

This method is also a Qt slot with the C++ signature void copy() .

把任何选中文本拷贝到剪贴板。

另请参阅 copyAvailable ().

QMimeData QPlainTextEdit.createMimeDataFromSelection ( self )

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 copied 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 () 函数。

QMenu QPlainTextEdit.createStandardContextMenu ( self )

This function creates the standard context menu which is shown when the user clicks on the line edit with the right mouse button. It is called from the default contextMenuEvent () handler. The popup menu's ownership is transferred to the caller.

QTextCharFormat QPlainTextEdit.currentCharFormat ( self )

Returns the char format that is used when inserting new text.

另请参阅 setCurrentCharFormat ().

QTextCursor QPlainTextEdit.cursorForPosition ( self , QPoint   pos )

返回 QTextCursor 在位置 pos (在视口坐标中)。

QRect QPlainTextEdit.cursorRect ( self , QTextCursor   cursor )

返回矩形 (在视口坐标中) 包括 cursor .

QRect QPlainTextEdit.cursorRect ( self )

返回矩形 (在视口坐标中) 包括 cursor of the text edit.

int QPlainTextEdit.cursorWidth ( self )

QPlainTextEdit.cut ( self )

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.

若没有选中文本,什么都不会发生。

另请参阅 copy () and paste ().

QTextDocument QPlainTextEdit.document ( self )

返回指向底层文档的指针。

另请参阅 setDocument ().

QString QPlainTextEdit.documentTitle ( self )

QPlainTextEdit.dragEnterEvent ( self , QDragEnterEvent   e )

重实现自 QWidget.dragEnterEvent ().

QPlainTextEdit.dragLeaveEvent ( self , QDragLeaveEvent   e )

重实现自 QWidget.dragLeaveEvent ().

QPlainTextEdit.dragMoveEvent ( self , QDragMoveEvent   e )

重实现自 QWidget.dragMoveEvent ().

QPlainTextEdit.dropEvent ( self , QDropEvent   e )

重实现自 QWidget.dropEvent ().

QPlainTextEdit.ensureCursorVisible ( self )

Ensures that the cursor is visible by scrolling the text edit if necessary.

另请参阅 centerCursor () 和 centerOnScroll .

bool QPlainTextEdit.event ( self , QEvent   e )

list-of-QTextEdit.ExtraSelection QPlainTextEdit.extraSelections ( self )

返回先前设置的额外选定。

另请参阅 setExtraSelections ().

bool QPlainTextEdit.find ( self , QString  exp , QTextDocument.FindFlags   options  = 0)

查找字符串的下一出现 exp ,使用 given options . Returns true if exp was found and changes the cursor to select the match; otherwise returns false.

QTextBlock QPlainTextEdit.firstVisibleBlock ( self )

Returns the first visible block.

另请参阅 blockBoundingRect ().

QPlainTextEdit.focusInEvent ( self , QFocusEvent   e )

重实现自 QWidget.focusInEvent ().

bool QPlainTextEdit.focusNextPrevChild ( self , bool  next )

重实现自 QWidget.focusNextPrevChild ().

QPlainTextEdit.focusOutEvent ( self , QFocusEvent   e )

重实现自 QWidget.focusOutEvent ().

QAbstractTextDocumentLayout.PaintContext QPlainTextEdit.getPaintContext ( self )

Returns the paint context for the 视口 (), useful only when reimplementing paintEvent ().

QPlainTextEdit.inputMethodEvent ( self , QInputMethodEvent )

重实现自 QWidget.inputMethodEvent ().

QVariant QPlainTextEdit.inputMethodQuery ( self , Qt.InputMethodQuery   property )

重实现自 QWidget.inputMethodQuery ().

QPlainTextEdit.insertFromMimeData ( self , QMimeData   source )

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.

QPlainTextEdit.insertPlainText ( self , QString  text )

This method is also a Qt slot with the C++ signature void insertPlainText(const QString&) .

方便槽,插入 text 在当前光标 position.

它相当于

 edit->textCursor().insertText(text);
			

bool QPlainTextEdit.isReadOnly ( self )

bool QPlainTextEdit.isUndoRedoEnabled ( self )

QPlainTextEdit.keyPressEvent ( self , QKeyEvent   e )

重实现自 QWidget.keyPressEvent ().

QPlainTextEdit.keyReleaseEvent ( self , QKeyEvent   e )

重实现自 QWidget.keyReleaseEvent ().

LineWrapMode QPlainTextEdit.lineWrapMode ( self )

QVariant QPlainTextEdit.loadResource ( self , int  type , QUrl   name )

加载指定资源通过给定 type and name .

This function is an extension of QTextDocument.loadResource ().

另请参阅 QTextDocument.loadResource ().

int QPlainTextEdit.maximumBlockCount ( self )

QPlainTextEdit.mergeCurrentCharFormat ( self , QTextCharFormat   modifier )

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 ().

QPlainTextEdit.mouseDoubleClickEvent ( self , QMouseEvent   e )

重实现自 QWidget.mouseDoubleClickEvent ().

QPlainTextEdit.mouseMoveEvent ( self , QMouseEvent   e )

重实现自 QWidget.mouseMoveEvent ().

QPlainTextEdit.mousePressEvent ( self , QMouseEvent   e )

重实现自 QWidget.mousePressEvent ().

QPlainTextEdit.mouseReleaseEvent ( self , QMouseEvent   e )

重实现自 QWidget.mouseReleaseEvent ().

QPlainTextEdit.moveCursor ( self , QTextCursor.MoveOperation   operation , QTextCursor.MoveMode   mode  = QTextCursor.MoveAnchor)

移动光标通过履行给定 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.

另请参阅 QTextCursor.movePosition ().

bool QPlainTextEdit.overwriteMode ( self )

QPlainTextEdit.paintEvent ( self , QPaintEvent   e )

重实现自 QWidget.paintEvent ().

QPlainTextEdit.paste ( self )

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 QPlainTextEdit can paste and how it is being pasted, reimplement the virtual canInsertFromMimeData () and insertFromMimeData () 函数。

另请参阅 cut () 和 copy ().

QPlainTextEdit.print_ ( self , QPrinter   printer )

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.

另请参阅 QTextDocument.print ().

QPlainTextEdit.redo ( self )

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.

另请参阅 undo ().

QPlainTextEdit.resizeEvent ( self , QResizeEvent   e )

重实现自 QWidget.resizeEvent ().

QPlainTextEdit.scrollContentsBy ( self , int  dx , int  dy )

重实现自 QAbstractScrollArea.scrollContentsBy ().

QPlainTextEdit.selectAll ( self )

This method is also a Qt slot with the C++ signature void selectAll() .

选择所有文本。

另请参阅 copy (), cut (),和 textCursor ().

QPlainTextEdit.setBackgroundVisible ( self , bool  visible )

QPlainTextEdit.setCenterOnScroll ( self , bool  enabled )

QPlainTextEdit.setCurrentCharFormat ( self , QTextCharFormat   format )

把新文本插入时使用的字符格式设为 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 ().

QPlainTextEdit.setCursorWidth ( self , int  width )

QPlainTextEdit.setDocument ( self , QTextDocument   document )

Makes document the new document of the text editor.

父级 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.

文档必须具有文档布局,继承 QPlainTextDocumentLayout (见 QTextDocument.setDocumentLayout ()).

另请参阅 document ().

QPlainTextEdit.setDocumentTitle ( self , QString  title )

QPlainTextEdit.setExtraSelections ( self , list-of-QTextEdit.ExtraSelection  selections )

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.

另请参阅 QTextEdit.ExtraSelection and extraSelections ().

QPlainTextEdit.setLineWrapMode ( self , LineWrapMode   mode )

QPlainTextEdit.setMaximumBlockCount ( self , int  maximum )

QPlainTextEdit.setOverwriteMode ( self , bool  overwrite )

QPlainTextEdit.setPlainText ( self , QString  text )

This method is also a Qt slot with the C++ signature void setPlainText(const QString&) .

QPlainTextEdit.setReadOnly ( self , bool  ro )

QPlainTextEdit.setTabChangesFocus ( self , bool  b )

QPlainTextEdit.setTabStopWidth ( self , int  width )

QPlainTextEdit.setTextCursor ( self , QTextCursor   cursor )

设置可见 cursor .

另请参阅 textCursor ().

QPlainTextEdit.setTextInteractionFlags ( self , Qt.TextInteractionFlags   flags )

QPlainTextEdit.setUndoRedoEnabled ( self , bool  enable )

QPlainTextEdit.setWordWrapMode ( self , QTextOption.WrapMode   policy )

QPlainTextEdit.showEvent ( self , QShowEvent )

重实现自 QWidget.showEvent ().

bool QPlainTextEdit.tabChangesFocus ( self )

int QPlainTextEdit.tabStopWidth ( self )

QTextCursor QPlainTextEdit.textCursor ( self )

返回副本为 QTextCursor that represents the currently visible cursor. Note that changes on the returned cursor do not affect QPlainTextEdit 的光标; 使用 setTextCursor () to update the visible cursor.

另请参阅 setTextCursor ().

Qt.TextInteractionFlags QPlainTextEdit.textInteractionFlags ( self )

QPlainTextEdit.timerEvent ( self , QTimerEvent   e )

QString QPlainTextEdit.toPlainText ( self )

QPlainTextEdit.undo ( self )

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.

另请参阅 redo ().

QPlainTextEdit.wheelEvent ( self , QWheelEvent   e )

重实现自 QWidget.wheelEvent ().

QTextOption.WrapMode QPlainTextEdit.wordWrapMode ( self )


Qt Signal Documentation

void blockCountChanged (int)

This is the default overload of this signal.

This signal is emitted whenever the block count changes. The new block count is passed in newBlockCount .

void copyAvailable (bool)

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 ().

void cursorPositionChanged ()

This is the default overload of this signal.

This signal is emitted whenever the position of the cursor 改变。

void modificationChanged (bool)

This is the default overload of this signal.

This signal is emitted whenever the content of the document changes in a way that affects the modification state. If changed is true, the document has been modified; otherwise it is false.

For example, calling setModified(false) on a document and then inserting text causes the signal to get emitted. If you undo that operation, causing the document to return to its original unmodified state, the signal will get emitted again.

void redoAvailable (bool)

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).

void selectionChanged ()

This is the default overload of this signal.

此信号被发射每当选定改变。

另请参阅 copyAvailable ().

void textChanged ()

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.

void undoAvailable (bool)

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).

void updateRequest (const QRect&,int)

This is the default overload of this signal.

This signal is emitted when the text document needs an update of the specified rect 。若文本被卷动, rect will cover the entire viewport area. If the text is scrolled vertically, dy carries the amount of pixels the viewport was scrolled.

The purpose of the signal is to support extra widgets in plain text edit subclasses that e.g. show line numbers, breakpoints, or other extra information.