QTextEdit 将 setLineWrapMode 设置为 NoWrap 会导致较大的延迟
QTextEdit setting setLineWrapMode to NoWrap causes large delay
我正在使用 QTextEdit
显示 Table。
当我尝试使用 setLineWrapMode(qtw.QTextEdit.NoWrap)
禁用换行时,显示 table 需要很长时间。
这是预期的行为吗?我怎样才能减少延迟?
最小工作示例
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())
我假设在循环中 for i in range (200):
每次 table 更改时,它都会检查 QTextEdit.NoWrap
是否得到遵守。同时,显然,它使此安装跨越当前存在的整个 table。
抱歉我的英语不好。
将 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())
我正在使用 QTextEdit
显示 Table。
当我尝试使用 setLineWrapMode(qtw.QTextEdit.NoWrap)
禁用换行时,显示 table 需要很长时间。
这是预期的行为吗?我怎样才能减少延迟?
最小工作示例
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())
我假设在循环中 for i in range (200):
每次 table 更改时,它都会检查 QTextEdit.NoWrap
是否得到遵守。同时,显然,它使此安装跨越当前存在的整个 table。
抱歉我的英语不好。
将 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())