【发布时间】:2016-01-27 13:06:36
【问题描述】:
我正在开发一个文本编辑器我正在使用 Qt4.8/Pyqt 特别是 QTextedit 对象,在 Windows 7 上使用 python 2.7 考虑以下代码(不是原始的)
def doReplaceAll(self):
# Replace all occurences without interaction
# Here I am just getting the replacement data
# from my UI so it will be different for you
old=self.searchReplaceWidget.ui.text.text()
new=self.searchReplaceWidget.ui.replaceWith.text()
# Beginning of undo block
cursor=self.editor.textCursor()
cursor.beginEditBlock()
# Use flags for case match
flags=QtGui.QTextDocument.FindFlags()
if self.searchReplaceWidget.ui.matchCase.isChecked():
flags=flags|QtGui.QTextDocument.FindCaseSensitively
# Replace all we can
while True:
# self.editor is the QPlainTextEdit
r=self.editor.find(old,flags)
if r:
qc=self.editor.textCursor()
if qc.hasSelection():
qc.insertText(new)
else:
break
# Mark end of undo block
cursor.endEditBlock()
这适用于几百行文本。但是当我有很多文本说 10000 到 100000 行文本替换时,所有的文本都非常慢到无法使用的程度,因为编辑器会减慢速度。 难道我做错了什么。为什么 QTextEdit 这么慢,我尝试了 QplaingTextEdit 也没有太多运气。有什么建议吗?
【问题讨论】:
-
您可以尝试将文本作为纯字符串获取,然后在其中进行替换(可能还需要对其进行优化,以便每次替换不会一次又一次地移动字符串的其余部分......)。
标签: python-2.7 pyqt qt4 qtextedit