I'm a novice attempting to write a Z-machine interpreter -- a specialized virtual machine for text adventures like Zork -- in Python, using PyQt4. I decided to subclass QTextEdit for the program's main interface, but I'm having trouble turning it into a widget that will do what I'm looking for. I have to be able to append text to it, accept user input on the same line as the last appended character, then append more text, and the user must never be allowed to edit any of the text previously appended by the program or entered by the user. To put it another way, I have to periodically make all text in the widget read only except for new text the user is typing at the end of it. Here's the code I've tried most recently:
class ZScreen(QTextEdit):
def __init__(self, parent=None):
super(QTextEdit, self).__init__(parent)
self.setUndoRedoEnabled(False)
self.setAcceptRichText(False)
self.readOnlyBefore = self.textCursor().position
def changeEvent(self, e):
if self.textCursor().position < self.readOnlyBefore:
self.setReadOnly(True)
else:
self.setReadOnly(False)
super(QTextEdit, self).changeEvent(e)
def printTo(self, text):
self.append(text)
self.moveCursor(QTextCursor.End)
self.readOnlyBefore = self.textCursor().position