• PyQt5 QTextBrowser刷新、上限、查找


    使用PyQt5做UI,我一直使用QTextBrowser作为LOG的输出界面。不知道对不对。感觉QTextBrowser是文本浏览器,就像txt一样是查看文本的。字面意思吧,好吧,我英文不好

    QTextBrowser刷新

    上代码

        def text_browser_show(self, mes):
            save_str = r"[{}]◆{}".format(datetime.now().strftime('%H:%M:%S.%f')[:-3], mes)
            with open(LogName, 'a') as src_f:
                print(save_str, file=src_f)
            self.textBrowser.append(save_str)
            my_cursor = self.textBrowser.textCursor()
            self.textBrowser.moveCursor(my_cursor.End)
            # sleep(0.05)  # 0:00:01.156
            QtWidgets.QApplication.processEvents(QtCore.QEventLoop.AllEvents)
    解析
            with open(LogName, 'a') as src_f:
                print(save_str, file=src_f)

    这个代码是用于向文件中写log的,本意是在想浏览器输出log的同时也将log写入文件中,如果不需要可忽略

    self.textBrowser.append(save_str)

    这段代码就是将mes添加到浏览器中,但是需注意浏览器的光标位置。append()属于QTextEdit槽函数(也不知道为什么QTextBrowser可以调用QTextEdit的槽函数,有可能他们都继承至PySide2.QtWidgets.QWidget)

    append(text)

    参数类型:

    text - str

    注意:

    The new paragraph appended will have the same character format and block format as the current paragraph, determined by the position of the cursor.

    将带有文本的新段落追加到文本编辑的末尾

    光标影响append()的插入位置,所以我们加入文本后需要将光标移动到指定位置,这里移动到文末(第一次没有文本,所以不需要移动)。

            my_cursor = self.textBrowser.textCursor()

    获取光标

    self.textBrowser.moveCursor(my_cursor.End)

    移动光标。

    textCursor()

    返回类型:

    PySide2.QtGui.QTextCursor

    返回表示当前可见光标的QTextCursor的副本。请注意,对返回的游标所做的更改不会影响QTextEdit的游标;使用setTextCursor()更新可见光标。

    moveCursor(operation[,mode=QTextCursor.MoveAnchor])

    参数类型:

    operation - MoveOperation

    mode - MoveMode

    通过执行给定的操作来移动光标。如果mode为KeepAnchor,则光标将选择其移动的文本。这与用户按住Shift键并使用光标键移动光标时获得的效果相同。

    这些都为QTextEdit的函数。所以我觉的应该用QTextEdit,然后设置readonly就可以了。不过走来了就记录下。

            # sleep(0.05)  # 0:00:01.156
            QtWidgets.QApplication.processEvents(QtCore.QEventLoop.AllEvents)

    sleep亲测有时需要有时不需要。可能跟添加文本长度有关。保险起见应添加上,不过值可设小一点。

    processEvents([flags=QEventLoop.AllEvents])

    参数类型:

    flags - ProcessEventsFlags

    maxtime - int

    注意:

    Unlike the processEvents() overload, this function also processes events that are posted while the function runs.

    此函数将重载processEvents()。为调用线程处理等待的事件数毫秒(毫秒)或直到没有更多要处理的事件(以时间较短者为准)。您可以在程序忙于执行长操作(例如,复制一个文件)。调用此函数仅处理调用线程的事件。

    设置QTextBrowser显示上限

    browser_document = self.textBrowser.document()
    browser_document.setMaximumBlockCount(100000)

    document()

    返回类型:

    PySide2.QtGui.QTextDocument

    注意:

    The editor does not take ownership of the document unless it is the document's parent object. The parent object of the provided document remains the owner of the object. If the previously assigned document is a child of the editor then it will be deleted.

    此属性保存文本编辑器的基础文档。

    setMaximumBlockCount(maximum)

    参数类型:

    maximum - int

    此属性指定文档中的块数限制。指定文档可能具有的最大块数。如果在文档中还有更多用此属性指定的块,则从文档开头删除这些块。负值或零值表示该文档可以包含无限数量的块,默认值为0。属性将限制立即应用于文档内容。设置此属性还会禁用撤消重做历史记录。此属性在带有表或框架的文档中未定义。

    可以理解为最大行数设置

    QTextBrowser查找

    self.textBrowser.scrollToAnchor('X')     # 查询某个关键字(只能将滑轮滚动到第一个查到的字符)

    scrollToAnchor(name)

    参数类型:

    name - str

    滚动文本编辑,以便显示具有给定名称的锚点;如果名称为空,已经可见或找不到,则不执行任何操作。

    没错,QTextEdit函数

    以前-好记性不如烂笔头 现在-好记性不如烂键盘
  • 相关阅读:
    Linux下安装配置SVN服务器,windows访问
    Zookeeper集群版搭建
    Zookeeper单机版启动
    Nginx-Session缓存一致性-memcached
    Nginx-配置多台Tomcat-反向代理
    Linux-tomcat-安装启动
    Linux-JDK-环境搭建安装
    Nginx-安装-运行访问页面
    Linux-虚拟机-克隆-学习
    解决CocosCreator 在微信小游戏中使用Socket.io 报错的问题
  • 原文地址:https://www.cnblogs.com/gexbooks/p/14866663.html
Copyright © 2020-2023  润新知