文本光标
1、 理论基础
- 通过文本光标,可以操作编辑文本文档对象
- 概念:
- 整个文本编辑器,其实就是为编辑这个文本文档,提供了一个可视化的界面
- 简单理解,可以比喻成一个doc文档,使用word软件打开了这个文档,就可以随意修改文档内容
- 获取文本文档的方法:
document()
:得到QTextDocument
textCursor()
:
2、 内容相关
2.1 添加内容
2.1.1 插入文本
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| |
| |
| text_cursor = tx.textCursor() |
| |
| text_cursor.insertHtml("<h1>这是一级标题<h2>") |
| text_cursor.insertText("这是普通文本内容") |
| |
| format_ = QTextCharFormat() |
| format_.setToolTip("你好呀,你看到这句话,就说明语法没关系") |
| format_.setFont(QFont("隶书")) |
| text_cursor.insertText("格式化文本", format_) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.1.2 插入图片
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| |
| text_cursor = tx.textCursor() |
| format_ = QTextImageFormat() |
| format_.setName("./open.jpg") |
| format_.setHeight(100) |
| format_.setWidth(100) |
| text_cursor.insertImage(format_) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.1.3 插入句子
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| |
| text_cursor = tx.textCursor() |
| qtd = QTextDocumentFragment.fromHtml("<h1>xxx</h1>") |
| text_cursor.insertFragment(qtd) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.1.4 插入列表
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| |
| text_cursor = tx.textCursor() |
| |
| |
| |
| |
| format_ = QTextListFormat() |
| format_.setIndent(4) |
| format_.setNumberPrefix("<<") |
| format_.setNumberSuffix(">>") |
| format_.setStyle(QTextListFormat.ListDecimal) |
| text_cursor.insertList(format_) |
| |
| w.show() |
| sys.exit(app.exec_()) |
QtextListFormat.Style
:定义左侧显示的图标
QTextListFormat.ListDisc
:圆圈
QTextListFormat.ListCircle
:空圆圈
QTextListFormat.ListSquare
:方块
QTextListFormat.ListDecimal
:十进制值按照升序排列
QTextListFormat.ListLowerAlpha
:小写拉丁字符按字母顺序排列
QTextListFormat.ListUpperAlpha
:大写拉丁字符按字母顺序排列
QTextListFormat.ListLowerRoman
:小写罗马数字
QTextListFormat.ListUpperRoman
:大写罗马数字
2.1.5 插入表格
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| |
| text_cursor = tx.textCursor() |
| |
| format_ = QTextTableFormat() |
| format_.setAlignment(Qt.AlignRight) |
| format_.setCellPadding(6) |
| format_.setCellSpacing(0) |
| format_.setColumnWidthConstraints([QTextLength(QTextLength.PercentageLength, 4), QTextLength(QTextLength.PercentageLength, 6)]) |
| |
| table = text_cursor.insertTable(3, 2, format_) |
| table.appendRows(3) |
| table.insertRows(0, 3) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.1.6 插入文本块
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| tx.setFocus() |
| |
| text_cursor = tx.textCursor() |
| format_ = QTextBlockFormat() |
| format_.setIndent(2) |
| format_.setAlignment(Qt.AlignLeft) |
| format_.setTopMargin(100) |
| text_cursor.insertBlock(format_) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.1.7 插入框架
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 500) |
| tx.setFocus() |
| |
| text_cursor = tx.textCursor() |
| format_ = QTextFrameFormat() |
| format_.setBorder(10) |
| format_.setBorderBrush(QColor("red")) |
| text_cursor.insertFrame(format_) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.2 设置和合并格式
| text_cursor.setBlockCharFormat(QTextCharFormat) |
| setBlockFormat(QTextBlockFormat) |
| setCharFormat(QTextCharFormat) |
| mergeBlockCharFormat(QTextBlockCharFormat) |
| mergeBlockFormat(QTextBlockFormat) |
| mergeCharFormat(QTextCharFormat) |
合并的效果是叠加的
2.3 获取内容和格式
| block() |
| block().text() |
| blockFormat() |
| blockCharFormat() |
| blockNumber() |
| charFormat() |
| currentFrame() |
| currentList() |
| currentTable() |
2.4 文本选中和清空
2.4.1 光标选中
| setPositon(int pos, QTextCursor.MoveMode=MoveAnchor) |
| movePosition(QTextCursor.MoveOperation, QTextCursor.MoveMode=MoveAnchor, int n = 1) |
| select(QTextCursor.SelectionType) |
QTextCursor.MoveMode
:
QTextCursor.MoveAnchor
: 将锚点移动到与光标本身相同的位置
QTextCursor.KeepAnchor
: 将锚点固定值哎原来的位置
QTextCursor.MoveOperation
: 内容比较多,请到官方文档查看
QTextCursor.SelectionType
:
QTextCursor.Document
: 选择整个文档
QTextCursor.BlockUnderCursor
:选择光标下的文本块
QTextCursor.LineUnderCursor
:选择光标下的文本行
QTextCursor.WordUnderCursor
:选择光标下的单词
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 400) |
| |
| |
| def btn_(): |
| text_cursor = tx.textCursor() |
| |
| |
| text_cursor.select(QTextCursor.LineUnderCursor) |
| tx.setTextCursor(text_cursor) |
| tx.setFocus() |
| |
| |
| btn = QPushButton("test", w) |
| btn.pressed.connect(btn_) |
| btn.move(203, 410) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.4.2 选中内容获取
| selectText() |
| selectionStart() |
| selectionEnd() |
| selection() |
| selectedTableCells() |
示例:
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 400) |
| |
| tx.textCursor().insertTable(5, 3) |
| |
| def btn_(): |
| text_cursor = tx.textCursor() |
| print(text_cursor.selectedText()) |
| print(text_cursor.selectionStart()) |
| print(text_cursor.selectionEnd()) |
| print(text_cursor.selection().toPlainText()) |
| print(text_cursor.selectedTableCells()) |
| |
| |
| |
| btn = QPushButton("test", w) |
| btn.pressed.connect(btn_) |
| btn.move(203, 410) |
| |
| w.show() |
| sys.exit(app.exec_()) |
2.4.3 选定内容删除和判定
| clearSelection() |
| hasSelection() |
| removeSelection() |
| |
| deleteChar() |
| deletePreviousChar() |
2.5 位置相关
| atBlockEnd() |
| atBlockStart() |
| atEnd() |
| atStart() |
| |
| columnNumber() |
| position() |
| positionBlock() |
2.6 开始和结束编辑标识
| |
| |
| |
| from PyQt5.Qt import * |
| import sys |
| |
| app = QApplication(sys.argv) |
| w = QWidget() |
| w.resize(500, 500) |
| tx = QTextEdit(w) |
| tx.resize(500, 400) |
| |
| |
| def btn_(): |
| text_cursor = tx.textCursor() |
| text_cursor.beginEditBlock() |
| text_cursor.insertText("hello") |
| text_cursor.insertText("world") |
| text_cursor.endEditBlock() |
| |
| |
| btn = QPushButton("test", w) |
| btn.pressed.connect(btn_) |
| btn.move(203, 410) |
| |
| w.show() |
| sys.exit(app.exec_()) |
通过这个方法,可以将多个内容输入合并为一个输入,方便对内容的后续操作
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器