QPlainTextEdit 如果它有一个空文本就认为它被修改了
QPlainTextEdit thinks it's modified if it has an empty text
我正在使用 PyQt 构建一个简单的 IDE,如果您加载一个空文件,会出现奇怪的错误。下面发布了一个小示例脚本:
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class TestApp(QtGui.QMainWindow):
def __init__(self, filename=None):
super(TestApp, self).__init__()
self._editor = QtGui.QPlainTextEdit()
self._editor.modificationChanged.connect(self._change_modified)
self.setCentralWidget(self._editor)
self._editor.setPlainText('a')
def _change_modified(self, have_change):
print(have_change)
if __name__ == '__main__':
a = QtGui.QApplication([])
app = TestApp()
app.show()
sys.exit(a.exec_())
正如预期的那样,这显示了带有纯文本编辑器的 window。一旦 setPlainText
方法被调用,编辑器就会发出两个事件:一个 modificationChanged
事件带有 changes=True
,第二个带有 changes=False
。
有点奇怪,但很好。
但是,如果将 setPlainText('a')
更改为 setPlainText('')
,则只会发出一个事件,这次是 changes=True
。更糟糕的是,在告诉编辑它没有用 setModified(False)
修改后,它坚持认为它已经以某种方式改变了。
有谁知道是什么原因造成的,我该如何解决这个问题?
更新:这似乎是一个错误并且也会影响 QPlainTextEdit.clear()
。
下面的解决方法在 QPlainTextEdit
周围放置了一个包装器来修复 clear()
和 setPlainText('')
.
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class TestApp(QtGui.QMainWindow):
def __init__(self, filename=None):
super(TestApp, self).__init__()
self._editor = PlainTextEdit()
self._editor.modificationChanged.connect(self._change_modified)
self.setCentralWidget(self._editor)
self._editor.setPlainText('')
def _change_modified(self, have_change):
print(have_change)
class PlainTextEdit(QtGui.QPlainTextEdit):
def clear(self):
self.selectAll()
cursor = self.textCursor()
cursor.removeSelectedText()
doc = self.document()
doc.clearUndoRedoStacks()
doc.setModified(False)
self.modificationChanged.emit(False)
def setPlainText(self, text):
if text:
super(PlainTextEdit, self).setPlainText(text)
else:
self.clear()
if __name__ == '__main__':
a = QtGui.QApplication([])
app = TestApp()
app.show()
sys.exit(a.exec_())
这是一个 Qt 错误,如果指示修改,直接的解决方法是检查空内容。
我正在使用 PyQt 构建一个简单的 IDE,如果您加载一个空文件,会出现奇怪的错误。下面发布了一个小示例脚本:
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class TestApp(QtGui.QMainWindow):
def __init__(self, filename=None):
super(TestApp, self).__init__()
self._editor = QtGui.QPlainTextEdit()
self._editor.modificationChanged.connect(self._change_modified)
self.setCentralWidget(self._editor)
self._editor.setPlainText('a')
def _change_modified(self, have_change):
print(have_change)
if __name__ == '__main__':
a = QtGui.QApplication([])
app = TestApp()
app.show()
sys.exit(a.exec_())
正如预期的那样,这显示了带有纯文本编辑器的 window。一旦 setPlainText
方法被调用,编辑器就会发出两个事件:一个 modificationChanged
事件带有 changes=True
,第二个带有 changes=False
。
有点奇怪,但很好。
但是,如果将 setPlainText('a')
更改为 setPlainText('')
,则只会发出一个事件,这次是 changes=True
。更糟糕的是,在告诉编辑它没有用 setModified(False)
修改后,它坚持认为它已经以某种方式改变了。
有谁知道是什么原因造成的,我该如何解决这个问题?
更新:这似乎是一个错误并且也会影响 QPlainTextEdit.clear()
。
下面的解决方法在 QPlainTextEdit
周围放置了一个包装器来修复 clear()
和 setPlainText('')
.
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
class TestApp(QtGui.QMainWindow):
def __init__(self, filename=None):
super(TestApp, self).__init__()
self._editor = PlainTextEdit()
self._editor.modificationChanged.connect(self._change_modified)
self.setCentralWidget(self._editor)
self._editor.setPlainText('')
def _change_modified(self, have_change):
print(have_change)
class PlainTextEdit(QtGui.QPlainTextEdit):
def clear(self):
self.selectAll()
cursor = self.textCursor()
cursor.removeSelectedText()
doc = self.document()
doc.clearUndoRedoStacks()
doc.setModified(False)
self.modificationChanged.emit(False)
def setPlainText(self, text):
if text:
super(PlainTextEdit, self).setPlainText(text)
else:
self.clear()
if __name__ == '__main__':
a = QtGui.QApplication([])
app = TestApp()
app.show()
sys.exit(a.exec_())
这是一个 Qt 错误,如果指示修改,直接的解决方法是检查空内容。