QTextEdit支持的功能包括:
字体样式和颜色:可以更改字体的颜色、字体大小和字体样式(粗体、斜体、下划线等)。
列表和表格:支持创建有序或无序列表,可以添加、删除和编辑表格。
超链接:可以添加超链接到文本中,并支持打开网页链接。
图片和媒体:可以插入图片和音频/视频文件。
撤销和重做:支持对编辑操作的撤销和重做。
查找和替换:可以查找并替换文本内容。
自动换行:可以自动将文本换行以适应编辑区域的大小。
此外,QTextEdit还提供了一些其他的配置选项,例如设置最大字符数、设置是否允许用户输入HTML代码等。
使用QTextEdit控件可以创建一个基本的文本编辑器,同时还可以与其他Qt控件一起使用,如QDockWidget(创建可停靠的文本编辑区域)和QTabWidget(创建多标签的文本编辑器)
创建 QTextEdit 对象:
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
text_edit = QTextEdit()
layout.addWidget(text_edit)
window.setLayout(layout)
window.show()
app.exec_()
设置和获取文本内容:
可以使用 setPlainText 方法设置文本内容,使用 toPlainText 方法获取文本内容
text_edit.setPlainText("Hello, PyQt5!")
text = text_edit.toPlainText()
设置只读和可编辑状态:
使用 setReadOnly 方法来设置文本编辑框是否为只读状态
text_edit.setReadOnly(True) # Make it read-only
text_edit.setReadOnly(False) # Make it editable
设置字体和格式:
使用 setFont 方法可以为文本内容设置字体,也可以使用 setFontPointSize 来设置字体大小。要更改选定文本的字体和格式,可以使用 QTextCursor 和 QTextCharFormat,如前面的示例所示。
格式化文本:
QTextEdit 支持丰富的文本格式化功能,例如粗体、斜体、下划线、颜色、对齐等。可以通过在文本编辑框中选择文本,然后使用工具栏或编程方式应用这些格式
from PyQt5.QtGui import QTextCursor
cursor = text_edit.textCursor()
char_format = QTextCharFormat()
char_format.setFontWeight(QFont.Bold)
cursor.mergeCharFormat(char_format)
滚动到特定位置:
使用 verticalScrollBar 和 horizontalScrollBar 方法可以获取滚动条,从而实现对 QTextEdit 的滚动控制。
信号和槽:
QTextEdit 支持多种信号和槽,用于处理文本变化、光标移动、文本选择等事件
text_edit.textChanged.connect(my_text_changed_handler)
text_edit.cursorPositionChanged.connect(my_cursor_moved_handler)
设置文本居中
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout
from PyQt5.QtGui import QTextCursor, QTextBlockFormat
from PyQt5.QtCore import Qt
class CenteredAllLinesTextDisplay(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Centered All Lines Text Display Example')
self.setGeometry(100, 100, 400, 300)
text = "Hello, PyQt5!\nThis is a centered text display example with all lines centered."
text_edit = QTextEdit(self)
text_edit.setPlainText(text)
text_edit.setReadOnly(True) # Make the text edit widget read-only
# Center align all lines
cursor = QTextCursor(text_edit.document())
cursor.movePosition(QTextCursor.Start)
while not cursor.atEnd():
cursor.movePosition(QTextCursor.StartOfBlock)
cursor.select(QTextCursor.BlockUnderCursor)
block_format = QTextBlockFormat()
block_format.setAlignment(Qt.AlignCenter)
cursor.mergeBlockFormat(block_format)
cursor.movePosition(QTextCursor.EndOfBlock)
cursor.movePosition(QTextCursor.NextBlock)
layout = QVBoxLayout()
layout.addWidget(text_edit)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = CenteredAllLinesTextDisplay()
window.show()
sys.exit(app.exec_())
设置字体大小
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QPushButton, QFontDialog
from PyQt5.QtGui import QTextCursor, QTextCharFormat, QFont
class FontSizeTextDisplay(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Font Size Text Display Example')
self.setGeometry(100, 100, 400, 300)
self.text_edit = QTextEdit(self)
self.text_edit.setPlainText("Hello, PyQt5!\nThis is a font size text display example.")
self.text_edit.setReadOnly(True) # Make the text edit widget read-only
change_font_size_button = QPushButton('Change Font Size', self)
change_font_size_button.clicked.connect(self.changeFontSize)
layout = QVBoxLayout()
layout.addWidget(self.text_edit)
layout.addWidget(change_font_size_button)
self.setLayout(layout)
def changeFontSize(self):
font, ok = QFontDialog.getFont(self)
if ok:
cursor = self.text_edit.textCursor()
char_format = QTextCharFormat()
char_format.setFontPointSize(font.pointSize())
cursor.mergeCharFormat(char_format)
self.text_edit.setCurrentFont(font)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = FontSizeTextDisplay()
window.show()
sys.exit(app.exec_())