QPlainTextEdit

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

Inheritance diagram of PySide2.QtWidgets.QPlainTextEdit

概要

函数

详细描述

介绍和概念

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 ,但为纯文本处理进行了优化。

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.

The shape of the mouse cursor on a QPlainTextEdit is IBeamCursor 默认情况下。它可以被改变透过 viewport() ‘s cursor property.

使用 QPlainTextEdit 作为显示 Widget

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

插入文本可用使用 QTextCursor 类或使用方便函数 insertPlainText() , appendPlainText() or paste() .

默认情况下,文本编辑在空白处自动换行以拟合在文本编辑 Widget 内。 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 ,可以指定是在空白处断开还是在任何地方断开采用 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.

只读键绑定

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

Keypresses

Action

UpArrow

向上移动一行。

DownArrow

向下移动一行。

LeftArrow

向左移动一字符。

RightArrow

向右移动一字符。

PageUp

向上移动一 (视口) 页。

PageDown

向下移动一 (视口) 页。

首页

移动到文本的起始。

End

移动到文本的结尾。

Alt+Wheel

水平卷动页面 (Wheel 是鼠标滚轮)。

Ctrl+Wheel

缩放文本。

Ctrl+A

选择所有文本。

使用 QPlainTextEdit 作为编辑器

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

文本选择的处理通过 QTextCursor 类,提供创建选择、检索文本内容或删除选定的功能。可以检索对应用户可见光标的对象使用 textCursor() method. If you want to set a selection in QPlainTextEdit just create one on a QTextCursor 对象然后使该光标成为可见光标使用 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() .

QPlainTextEdit holds a 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.

句法高亮

编辑键绑定

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

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

水平卷动页面 (Wheel 是鼠标滚轮)。

Ctrl+Wheel

缩放文本。

要选择 (标记) 文本,在按住 Shift 键的同时按下某一移动按键,例如, Shift+Right Arrow 将选择右侧字符,而 Shift+Ctrl+Right Arrow 将选择右侧单词,等等。

和 QTextEdit 的差异

QPlainTextEdit is a thin class, implemented by using most of the technology that is behind QTextEdit and QTextDocument 。其性能优于 QTextEdit stem mostly from using a different and simplified text layout called QPlainTextDocumentLayout on the text document (see 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() ).

class QPlainTextEdit ( [ parent=None ] )

QPlainTextEdit(text[, parent=None])

param parent

QWidget

param text

unicode

构造空 QPlainTextEdit 采用父级 parent .

PySide2.QtWidgets.QPlainTextEdit. LineWrapMode

常量

描述

QPlainTextEdit.NoWrap

QPlainTextEdit.WidgetWidth

PySide2.QtWidgets.QPlainTextEdit. anchorAt ( pos )
参数

pos QPoint

返回类型

unicode

返回锚点引用在位置 pos ,或空字符串若该点不存在锚点。

PySide2.QtWidgets.QPlainTextEdit. appendHtml ( html )
参数

html – unicode

追加新段落采用 html 到文本编辑末尾。

appendPlainText()

PySide2.QtWidgets.QPlainTextEdit. appendPlainText ( text )
参数

text – unicode

追加新段落采用 text 到文本编辑末尾。

另请参阅

appendHtml()

PySide2.QtWidgets.QPlainTextEdit. backgroundVisible ( )
返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. blockBoundingGeometry ( block )
参数

block QTextBlock

返回类型

QRectF

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

PySide2.QtWidgets.QPlainTextEdit. blockBoundingRect ( block )
参数

block QTextBlock

返回类型

QRectF

Returns the bounding rectangle of the text block in the block’s own coordinates.

PySide2.QtWidgets.QPlainTextEdit. blockCount ( )
返回类型

int

PySide2.QtWidgets.QPlainTextEdit. blockCountChanged ( newBlockCount )
参数

newBlockCount int

PySide2.QtWidgets.QPlainTextEdit. canInsertFromMimeData ( source )
参数

source QMimeData

返回类型

bool

此函数返回 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.

PySide2.QtWidgets.QPlainTextEdit. canPaste ( )
返回类型

bool

返回是否可以把文本从剪贴板粘贴到 textedit。

PySide2.QtWidgets.QPlainTextEdit. centerCursor ( )

卷动文档以使光标垂直居中。

PySide2.QtWidgets.QPlainTextEdit. centerOnScroll ( )
返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. clear ( )

删除文本编辑中的所有文本。

注意事项:

PySide2.QtWidgets.QPlainTextEdit. contentOffset ( )
返回类型

QPointF

Returns the content’s origin in viewport coordinates.

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.

PySide2.QtWidgets.QPlainTextEdit. copy ( )

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

另请参阅

copyAvailable()

PySide2.QtWidgets.QPlainTextEdit. copyAvailable ( b )
参数

b bool

PySide2.QtWidgets.QPlainTextEdit. createMimeDataFromSelection ( )
返回类型

QMimeData

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 object is passed to the caller. The selection can be retrieved by using the textCursor() 函数。

PySide2.QtWidgets.QPlainTextEdit. createStandardContextMenu ( )
返回类型

QMenu

此函数创建要展示的标准上下文菜单,当用户采用鼠标右键点击文本编辑时。它被调用从默认 contextMenuEvent() handler. The popup menu’s ownership is transferred to the caller.

We recommend that you use the ( QPoint ) 版本,启用用户点击位置敏感动作。

PySide2.QtWidgets.QPlainTextEdit. createStandardContextMenu ( position )
参数

position QPoint

返回类型

QMenu

此函数创建要展示的标准上下文菜单,当用户采用鼠标右键点击文本编辑时。它被调用从默认 contextMenuEvent() handler and it takes the position in document coordinates 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.

PySide2.QtWidgets.QPlainTextEdit. currentCharFormat ( )
返回类型

QTextCharFormat

返回插入新文本时使用的字符格式。

PySide2.QtWidgets.QPlainTextEdit. cursorForPosition ( pos )
参数

pos QPoint

返回类型

QTextCursor

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

PySide2.QtWidgets.QPlainTextEdit. cursorPositionChanged ( )
PySide2.QtWidgets.QPlainTextEdit. cursorRect ( cursor )
参数

cursor QTextCursor

返回类型

QRect

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

PySide2.QtWidgets.QPlainTextEdit. cursorRect ( )
返回类型

QRect

返回包括文本编辑光标的矩形 (在视口坐标中)。

PySide2.QtWidgets.QPlainTextEdit. cursorWidth ( )
返回类型

int

另请参阅

setCursorWidth()

PySide2.QtWidgets.QPlainTextEdit. cut ( )

把选中文本拷贝到剪贴板,并将其从文本编辑中删除。

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

另请参阅

copy() paste()

PySide2.QtWidgets.QPlainTextEdit. doSetTextCursor ( cursor )
参数

cursor QTextCursor

This provides a hook for subclasses to intercept cursor changes.

PySide2.QtWidgets.QPlainTextEdit. document ( )
返回类型

QTextDocument

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

另请参阅

setDocument()

PySide2.QtWidgets.QPlainTextEdit. documentTitle ( )
返回类型

unicode

PySide2.QtWidgets.QPlainTextEdit. ensureCursorVisible ( )

通过卷动文本编辑确保光标是可见的,若有必要。

PySide2.QtWidgets.QPlainTextEdit. extraSelections ( )
返回类型

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

PySide2.QtWidgets.QPlainTextEdit. find ( exp [ , options=QTextDocument.FindFlags() ] )
参数
  • exp QRegExp

  • options FindFlags

返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. find ( exp [ , options=QTextDocument.FindFlags() ] )
参数
  • exp QRegularExpression

  • options FindFlags

返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. find ( exp [ , options=QTextDocument.FindFlags() ] )
参数
  • exp – unicode

  • options FindFlags

返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. firstVisibleBlock ( )
返回类型

QTextBlock

Returns the first visible block.

PySide2.QtWidgets.QPlainTextEdit. getPaintContext ( )
返回类型

PaintContext

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

PySide2.QtWidgets.QPlainTextEdit. inputMethodQuery ( query , argument )
参数
  • query InputMethodQuery

  • argument – object

返回类型

object

PySide2.QtWidgets.QPlainTextEdit. insertFromMimeData ( source )
参数

source QMimeData

This function inserts the contents of the MIME data object, specified by 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.

PySide2.QtWidgets.QPlainTextEdit. insertPlainText ( text )
参数

text – unicode

方便槽,插入 text 在当前光标位置。

它相当于

edit.textCursor().insertText(text)
												
PySide2.QtWidgets.QPlainTextEdit. isReadOnly ( )
返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. isUndoRedoEnabled ( )
返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. lineWrapMode ( )
返回类型

LineWrapMode

另请参阅

setLineWrapMode()

PySide2.QtWidgets.QPlainTextEdit. loadResource ( type , name )
参数
  • type int

  • name QUrl

返回类型

object

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

This function is an extension of loadResource() .

另请参阅

loadResource()

PySide2.QtWidgets.QPlainTextEdit. maximumBlockCount ( )
返回类型

int

PySide2.QtWidgets.QPlainTextEdit. mergeCurrentCharFormat ( modifier )
参数

modifier QTextCharFormat

Merges the properties specified in modifier into the current character format by calling mergeCharFormat on the editor’s cursor. If the editor has a selection then the properties of modifier are directly applied to the selection.

另请参阅

mergeCharFormat()

PySide2.QtWidgets.QPlainTextEdit. modificationChanged ( arg__1 )
参数

arg__1 bool

PySide2.QtWidgets.QPlainTextEdit. moveCursor ( operation [ , mode=QTextCursor.MoveAnchor ] )
参数
  • operation MoveOperation

  • mode MoveMode

移动光标通过履行给定 operation .

mode is KeepAnchor ,光标选择由它移动覆盖的文本。这与用户达成的效果相同,当按下 Shift 键和采用光标键移动光标时。

另请参阅

movePosition()

PySide2.QtWidgets.QPlainTextEdit. overwriteMode ( )
返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. paste ( )

把剪贴板文本粘贴到文本编辑当前光标位置处。

若剪贴板中没有文本,什么都不发生。

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

PySide2.QtWidgets.QPlainTextEdit. placeholderText ( )
返回类型

unicode

PySide2.QtWidgets.QPlainTextEdit. print_ ( printer )
参数

printer QPagedPaintDevice

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 Selection as print range.

另请参阅

print()

PySide2.QtWidgets.QPlainTextEdit. redo ( )

重做上一操作。

If there is no operation to redo, i.e. there is no redo step in the undo/redo history, nothing happens.

另请参阅

undo()

PySide2.QtWidgets.QPlainTextEdit. redoAvailable ( b )
参数

b bool

PySide2.QtWidgets.QPlainTextEdit. selectAll ( )

选择所有文本。

PySide2.QtWidgets.QPlainTextEdit. selectionChanged ( )
PySide2.QtWidgets.QPlainTextEdit. setBackgroundVisible ( visible )
参数

visible bool

PySide2.QtWidgets.QPlainTextEdit. setCenterOnScroll ( enabled )
参数

enabled bool

另请参阅

centerOnScroll()

PySide2.QtWidgets.QPlainTextEdit. setCurrentCharFormat ( format )
参数

format QTextCharFormat

把新文本插入时使用的字符格式设为 format 通过调用 setCharFormat() on the editor’s cursor. If the editor has a selection then the char format is directly applied to the selection.

PySide2.QtWidgets.QPlainTextEdit. setCursorWidth ( width )
参数

width int

另请参阅

cursorWidth()

PySide2.QtWidgets.QPlainTextEdit. setDocument ( document )
参数

document QTextDocument

Makes document the new document of the text editor.

父级 QObject 文档的提供者仍然是对象的所有者。若当前文档是文本编辑器的子级,则将其删除。

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

另请参阅

document()

PySide2.QtWidgets.QPlainTextEdit. setDocumentTitle ( title )
参数

title – unicode

另请参阅

documentTitle()

PySide2.QtWidgets.QPlainTextEdit. setExtraSelections ( selections )
参数

selections

此函数允许采用给定颜色临时标记某些文档区域,指定通过 selections 。例如:这在编程编辑器中可以是有用的,采用给定背景颜色标记整行文本以指示断点的存在。

另请参阅

ExtraSelection extraSelections()

PySide2.QtWidgets.QPlainTextEdit. setLineWrapMode ( mode )
参数

mode LineWrapMode

另请参阅

lineWrapMode()

PySide2.QtWidgets.QPlainTextEdit. setMaximumBlockCount ( maximum )
参数

maximum int

PySide2.QtWidgets.QPlainTextEdit. setOverwriteMode ( overwrite )
参数

overwrite bool

另请参阅

overwriteMode()

PySide2.QtWidgets.QPlainTextEdit. setPlaceholderText ( placeholderText )
参数

placeholderText – unicode

另请参阅

placeholderText()

PySide2.QtWidgets.QPlainTextEdit. setPlainText ( text )
参数

text – unicode

把文本编辑的文本更改为字符串 text 。移除任何以前文本。

text 被解释为纯文本。

注意事项:

另请参阅

toPlainText()

PySide2.QtWidgets.QPlainTextEdit. setReadOnly ( ro )
参数

ro bool

另请参阅

isReadOnly()

PySide2.QtWidgets.QPlainTextEdit. setTabChangesFocus ( b )
参数

b bool

另请参阅

tabChangesFocus()

PySide2.QtWidgets.QPlainTextEdit. setTabStopDistance ( distance )
参数

distance qreal

另请参阅

tabStopDistance()

PySide2.QtWidgets.QPlainTextEdit. setTabStopWidth ( width )
参数

width int

注意

此函数被弃用。

另请参阅

tabStopWidth()

PySide2.QtWidgets.QPlainTextEdit. setTextCursor ( cursor )
参数

cursor QTextCursor

设置可见 cursor .

另请参阅

textCursor()

PySide2.QtWidgets.QPlainTextEdit. setTextInteractionFlags ( flags )
参数

flags TextInteractionFlags

PySide2.QtWidgets.QPlainTextEdit. setUndoRedoEnabled ( enable )
参数

enable bool

PySide2.QtWidgets.QPlainTextEdit. setWordWrapMode ( policy )
参数

policy WrapMode

另请参阅

wordWrapMode()

PySide2.QtWidgets.QPlainTextEdit. tabChangesFocus ( )
返回类型

bool

PySide2.QtWidgets.QPlainTextEdit. tabStopDistance ( )
返回类型

qreal

PySide2.QtWidgets.QPlainTextEdit. tabStopWidth ( )
返回类型

int

注意

此函数被弃用。

另请参阅

setTabStopWidth()

PySide2.QtWidgets.QPlainTextEdit. textChanged ( )
PySide2.QtWidgets.QPlainTextEdit. textCursor ( )
返回类型

QTextCursor

返回副本为 QTextCursor 表示当前可见光标。注意:改变返回光标不影响 QPlainTextEdit ‘s cursor; use setTextCursor() to update the visible cursor.

另请参阅

setTextCursor()

PySide2.QtWidgets.QPlainTextEdit. textInteractionFlags ( )
返回类型

TextInteractionFlags

PySide2.QtWidgets.QPlainTextEdit. toPlainText ( )
返回类型

unicode

把文本编辑的文本作为纯文本返回。

另请参阅

setPlainText()

PySide2.QtWidgets.QPlainTextEdit. undo ( )

撤消上一操作。

若没有要撤消的操作 (即:撤消/重做历史中没有撤消步骤),什么都不发生。

另请参阅

redo()

PySide2.QtWidgets.QPlainTextEdit. undoAvailable ( b )
参数

b bool

PySide2.QtWidgets.QPlainTextEdit. updateRequest ( rect , dy )
参数
  • rect QRect

  • dy int

PySide2.QtWidgets.QPlainTextEdit. wordWrapMode ( )
返回类型

WrapMode

另请参阅

setWordWrapMode()

PySide2.QtWidgets.QPlainTextEdit. zoomIn ( [ range=1 ] )
参数

range int

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

PySide2.QtWidgets.QPlainTextEdit. zoomInF ( range )
参数

range float

PySide2.QtWidgets.QPlainTextEdit. zoomOut ( [ range=1 ] )
参数

range int

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