PySide6 文本编辑器(QPlainTextEdit)实现查找功能——重构版本

PySide6 文本编辑器(QPlainTextEdit)实现查找功能------重构版本

核心
  1. 文本文档实例(QTextDocument)用于传入文本光标(QTextCursor)
  2. 正则表达式(QRegularExpression) 用于匹配查找内容
  3. 文本文档的查找方法(QPlainTextEdit.find)
  4. 将光标移动到指定的位置(QPlainTextEdit.setTextCursor
  5. 一个全局变量用于记录上一个文本光标的位置,一个局部变量用于记录下一个文本光标的位置

可选
查找标志

重构区别
  1. 设置用户界面代码更加简洁
  2. 实现基础的查找上一个(不支持循环)和下一个
  3. 未实现不区分大小写

旧版

代码示例
py 复制代码
# coding = utf-8

from PySide6.QtWidgets import (QApplication, QWidget, QLineEdit,QPlainTextEdit, QVBoxLayout,
                               QPushButton,QCheckBox)
from PySide6.QtCore import QRegularExpression
from PySide6.QtGui import QTextCursor,QTextDocument
import sys



class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUi()
        self.bindEvent()
        self.lasterCursor = None

    def setupUi(self):
        """设置用户界面
        """
        self.setWindowTitle("测试查找")

        # 主窗口使用垂直布局
        vBoxLayout = QVBoxLayout(self)

        # 创建文本编辑器 设置文本 行文本编辑器 按钮与 勾选框
        self.mytext = QPlainTextEdit()
        self.mytext.setPlainText(
                                "十年生死两茫茫,写程序,到天亮。\n"
                                 "千行代码,Bug何处藏。\n"
                                 "纵使上线又怎样,朝令改,夕断肠。"
                                 "\n相顾无言,惟有泪千行。\n "
                                 "每晚灯火阑珊处,夜难寐,加班狂。"
                                )   
        self.findLineEdit = QLineEdit()
        self.downPushButton = QPushButton("下一个!")
        self.upPushButton = QPushButton("上一个")
        self.caseSensitiveCheck = QCheckBox("区分大小写")
        
        vBoxLayout.addWidget(self.mytext)
        vBoxLayout.addWidget(self.downPushButton)
        vBoxLayout.addWidget(self.upPushButton)
        vBoxLayout.addWidget(self.caseSensitiveCheck)
        vBoxLayout.addWidget(self.findLineEdit)

    def bindEvent(self):
        self.downPushButton.clicked.connect(self.findNextText)
        self.upPushButton.clicked.connect(self.findPreviousText)

    def findNextText(self):
        """查找下一个文本
        """
        doc = self.mytext.document()
        searchText = self.findLineEdit.text()

        if not self.lasterCursor:
            self.lasterCursor = QTextCursor(doc)
        
        regex = QRegularExpression(searchText)
        nextCusror= doc.find(regex,self.lasterCursor)
        
        self.lasterCursor = nextCusror
        self.mytext.setTextCursor(nextCusror)
    
    def findPreviousText(self):
        """查找上一个文本
        """
        doc = self.mytext.document()
        searchText = self.findLineEdit.text()
        self.mytext.setTextCursor( self.lasterCursor)

        if not self.lasterCursor:
            self.lasterCursor = QTextCursor(doc)

        regex = QRegularExpression(searchText)
        prevCusor = doc.find(regex,self.lasterCursor,QTextDocument.FindFlag.FindBackward)
        
        self.lasterCursor = prevCusor
        self.mytext.setTextCursor(prevCusor)

 self.mytext.document().find(regex,self.lasterCursor,QTextDocument.FindFlag.FindCaseSensitively)
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    my = MyWidget()
    my.show()
    sys.exit(app.exec())

运行效果


相关推荐
兵慌码乱13 小时前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
luckdewei16 小时前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python
aqi001 天前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn1 天前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵2 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup112 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi002 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵2 天前
用 Python 实现 Take-Away 游戏
python·游戏