gpt4 book ai didi

python - 突出显示 QTextEdit 文档上的行

转载 作者:行者123 更新时间:2023-12-01 06:26:01 24 4
gpt4 key购买 nike

尝试创建一种方法(使用 PyQt5 和 Python 3)来突出显示 QTextEdit 小部件文档中给定的行号。下面是尝试的代码(非常感谢之前在 stackoverflow 中回答过类似问题的人):

from PyQt5.QtCore import Qt, QTimer, QEventLoop
from PyQt5.QtGui import QTextBlockFormat, QTextBlock, QTextCursor
from PyQt5.QtWidgets import QWidget, QApplication, QTextEdit, QVBoxLayout

sample = """
Test document...
This is Line 2
This is Line 3
Explanation:
This is an explanation section. Here we explain.
Explanation section ends.
Back to body of document.
This is Line 8.
This is the last line.
"""


class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.editor = QTextEdit(self)
self.editor.setText(sample)

self.format_normal = QTextBlockFormat()
self.format_normal.setBackground(Qt.white)

self.highlight_format = QTextBlockFormat()
self.highlight_format.setBackground(Qt.yellow)

self.cursor = self.editor.textCursor()

layout = QVBoxLayout(self)
layout.addWidget(self.editor)

def setLineFormat(self, lineNumber, format):
""" Sets the highlighting of a given line number in the QTextEdit"""
#self.cursor.clearSelection()
self.cursor.select(QTextCursor.Document)
self.cursor.setBlockFormat(self.format_normal)

self.cursor = QTextCursor(self.editor.document().findBlockByNumber(lineNumber))
self.cursor.setBlockFormat(format)

def cycle_through_lines(self):
""" Cycles through specified lines. """
for ii in range(2, 8):
self.setLineFormat(ii, self.highlight_format)
self.pause(1000)

def pause(self, duration):
""" Provides a pause of a specified duration. """
loop = QEventLoop()
QTimer.singleShot(duration, loop.quit)
loop.exec_()


if __name__ == '__main__':

import sys
app = QApplication(sys.argv)
window = Window()
window.setGeometry(500, 150, 300, 300)
window.show()
window.cycle_through_lines()
sys.exit(app.exec_())

问题:

  • 为什么不需要实例化一个新的光标来选择整个文档(重置突出显示时),但是当需要选择单行时,需要一个新的 QTextCursor() 实例被创造?

  • 有没有办法在不创建新实例的情况下选择单行?

  • 如果文档很长并且需要选择大量行,这种方法会产生内存问题吗?

大概垃圾收集器会处理这个问题,但我只是想更好地了解幕后的细节。

最佳答案

  • 为什么不需要实例化一个新的光标来选择整个文档(重置突出显示时),但是当需要选择单行时,需要一个新的 QTextCursor() 实例被创造?

    因为选择整个文档不需要像选择行、单词等那样具有特殊信息。因此,从 QTextEdit 获取的任何 QTextCursor(更准确地说,与 QTextEdit 关联的 QTextDocument 中的任何 QTextCursor)都将允许您选择整个文档。

  • 有没有办法在不创建新实例的情况下选择单行?

    选择一行需要QTextCursor具有行首、行尾等特殊信息,因此必须基于QTextBlock构建QTextCursor。

  • 如果文档很长并且需要选择大量行,这种方法会产生内存问题吗?

    不,不会产生内存问题,因为在您的情况下,您将其分配给同一个对象,但我仍然更喜欢使用以下方法

    def setLineFormat(self, lineNumber, format):
    """ Sets the highlighting of a given line number in the QTextEdit"""
    cursor = self.editor.textCursor()
    cursor.select(QTextCursor.Document)
    cursor.setBlockFormat(self.format_normal)<p></p>

    cursor = QTextCursor(self.editor.document().findBlockByNumber(lineNumber))
    cursor.setBlockFormat(format)

    在我的方法中,QTextCursor 是局部变量,当方法执行完成时,这些变量将被消除。

关于python - 突出显示 QTextEdit 文档上的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60139804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com