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

运行效果


相关推荐
郝学胜-神的一滴2 小时前
Effective Python 第39条:通过@classmethod多态来构造同一体系中的各类对象
开发语言·python·程序人生·软件工程
文火冰糖的硅基工坊3 小时前
[人工智能-综述-18]:AI重构千行百业的技术架构
大数据·人工智能·重构·架构·系统架构·制造·产业链
IT森林里的程序猿3 小时前
基于Python的招聘信息可视化分析系统
开发语言·python
我命由我123453 小时前
Photoshop - Photoshop 工具栏(4)套索工具
经验分享·笔记·学习·ui·职场和发展·职场·photoshop
我命由我123453 小时前
Photoshop - Photoshop 更改图像大小
笔记·学习·ui·职场和发展·职场发展·photoshop·ps
卷Java3 小时前
用户权限控制功能实现说明
java·服务器·开发语言·数据库·servlet·微信小程序·uni-app
Derrick__13 小时前
Python常用三方模块——psutil
开发语言·python
winrisef3 小时前
删除无限递归文件夹
java·ide·python·pycharm·系统安全
悦悦子a啊4 小时前
Java面向对象练习:Person类继承与排序
java·开发语言·python