PyQt:QPlainTextEdit 中的修改出现星号问题
我在使用 QPlainTextEdit 时遇到问题。我希望每当内容被修改时标题栏中就会出现“内容已被修改”星号。
在下面的示例中,键入几个字母。星号按其应有的方式显示。按 Ctrl+S,星号就会消失。但是,如果您再输入几个字母...为什么星号不再出现?
import os, sys
from PyQt4 import QtGui, QtCore
class MyTextEdit(QtGui.QPlainTextEdit):
def __init__(self):
QtGui.QPlainTextEdit.__init__(self)
save_seq = QtGui.QKeySequence.Save
self.save_shortcut = QtGui.QShortcut(save_seq, self, self.save)
QtCore.QObject.connect(self,
QtCore.SIGNAL("modificationChanged(bool)"),
self.on_change)
def on_change(self, is_modified):
print "on_change"
window.setWindowModified(is_modified)
def save(self):
window.setWindowModified(False)
#
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
edit = MyTextEdit()
window.setCentralWidget(edit)
window.setWindowTitle("None [*]")
window.show()
app.exec_()
I'm having a problem with a QPlainTextEdit. I want the "contents have been modified" asterisk to appear in the title bar whenever the contents have been modified.
In the example below, type a few letters. The asterisk appears as it should. Hit Ctrl+S, the asterisk disappears as it should. But then if you type a few more letters... why doesn't the asterisk appear again?
import os, sys
from PyQt4 import QtGui, QtCore
class MyTextEdit(QtGui.QPlainTextEdit):
def __init__(self):
QtGui.QPlainTextEdit.__init__(self)
save_seq = QtGui.QKeySequence.Save
self.save_shortcut = QtGui.QShortcut(save_seq, self, self.save)
QtCore.QObject.connect(self,
QtCore.SIGNAL("modificationChanged(bool)"),
self.on_change)
def on_change(self, is_modified):
print "on_change"
window.setWindowModified(is_modified)
def save(self):
window.setWindowModified(False)
#
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
edit = MyTextEdit()
window.setCentralWidget(edit)
window.setWindowTitle("None [*]")
window.show()
app.exec_()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,想通了。问题是在 save 方法中我应该调用 self.document().setModified(False) 而不是 window.setWindowModified(False)
Never mind, figured it out. The problem was that in the save method I should've been calling self.document().setModified(False) instead of window.setWindowModified(False)