QTextEdit将setLineWrapMode设置为NoWrap会导致较大的延迟

2024-09-28 21:59:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用QTextEdit显示一个表

当我试图使用setLineWrapMode(qtw.QTextEdit.NoWrap)禁用换行时,显示表格需要花费大量时间

这是预期的行为吗?我怎样才能减少延误

最小工作示例

import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc


class MainWindow(qtw.QMainWindow):

    def __init__(self):
        super().__init__()
        
        textedit = qtw.QTextEdit()
        textedit.setReadOnly(True)
        
        textedit.setLineWrapMode(qtw.QTextEdit.NoWrap) #Commenting this line removes the delay
        
        headers = ["#", "Name", "Address","Website"]
        cursor = textedit.textCursor()
        
        
        tableFormat = qtg.QTextTableFormat()
        table=cursor.insertTable(201, 4, tableFormat)
        
        for header in headers:
            cursor.insertText(header)
            cell=table.cellAt(cursor)
            format = cell.format()
            format.setBackground(qtg.QColor("#CCE8FF"))
            cell.setFormat(format)
            cursor.movePosition(qtg.QTextCursor.NextCell)
        
        for i in range(200):
            cursor.insertText(str(i+1))
            cursor.movePosition(qtg.QTextCursor.NextCell)
            cursor.insertText('David')
            cursor.movePosition(qtg.QTextCursor.NextCell)
            cursor.insertText('6th Lane, 4th street, 9th state')
            cursor.movePosition(qtg.QTextCursor.NextCell)
            cursor.insertHtml('<a href="www.google.com">Google</a>')
            cursor.movePosition(qtg.QTextCursor.NextCell)
        
        
        self.setCentralWidget(textedit)
        self.show()


if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec())

Tags: fromimportformatassyscursorpyqt5textedit
1条回答
网友
1楼 · 发布于 2024-09-28 21:59:13

我假设在循环中for i in range (200): 每次表更改时,它都会检查是否遵守了QTextEdit.NoWrap。同时,显然,它在当前存在的整个表中进行了此安装。 对不起,我的英语不好

textedit.setLineWrapMode(qtw.QTextEdit.NoWrap)行放在__init__方法的最后一行

import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc


class MainWindow(qtw.QMainWindow):
    def __init__(self):
        super().__init__()
        
        textedit = qtw.QTextEdit()
        textedit.setReadOnly(True)
        
#        textedit.setLineWrapMode(qtw.QTextEdit.NoWrap) # Commenting this line removes the delay
        
        headers = ["#", "Name", "Address","Website"]
        cursor = textedit.textCursor()
        
        tableFormat = qtg.QTextTableFormat()
        table=cursor.insertTable(201, 4, tableFormat)
        
        for header in headers:
            cursor.insertText(header)
            cell=table.cellAt(cursor)
            format = cell.format()
            format.setBackground(qtg.QColor("#CCE8FF"))
            cell.setFormat(format)
            cursor.movePosition(qtg.QTextCursor.NextCell)
        
        for i in range(200):
            cursor.insertText(str(i+1))
            cursor.movePosition(qtg.QTextCursor.NextCell)
            cursor.insertText('David')
            cursor.movePosition(qtg.QTextCursor.NextCell)
            cursor.insertText('6th Lane, 4th street, 9th state')
            cursor.movePosition(qtg.QTextCursor.NextCell)
            cursor.insertHtml('<a href="www.google.com">Google</a>')
            cursor.movePosition(qtg.QTextCursor.NextCell)
        
        self.setCentralWidget(textedit)
#        self.show()

        textedit.setLineWrapMode(qtw.QTextEdit.NoWrap)                    # +++


if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec())

enter image description here

相关问题 更多 >