[英]QTextEdit: Overwrite Ctrl-Z with undo() in PyQt4
I subclassed QTextEdit 我将QTextEdit子类化
class Q_My_TextEdit(QtGui.QTextEdit):
def __init__(self, *args):
QtGui.QTextEdit.__init__(self, *args)
def undo(self):
print("undo")
#my own undo code
In my other class: 在我的另一堂课中:
self.textedit=Q_My_TextEdit()
def keyPressEvent(self,event):
if event.key()==(Qt.Key_Control and Qt.Key_Z):
self.textedit.undo()
If you type some text into your QTextEdit, and hit CTRL-Z, it gets undo-ed, but without calling the function "undo". 如果您在QTextEdit中键入一些文本,然后按CTRL-Z,它将被撤消,但不会调用函数“ undo”。 So how does it work in detail?
那么它如何详细工作?
The background is, in a second step I want to set new Text (setText()), so the undo stack is deleted. 背景是,在第二步中,我想设置新文本(setText()),因此撤消堆栈被删除。 I already have running code that makes the undo itself, but I can't trigger it on CTRL-Z, because with the "Z" the keyshortcut is somehow reserved.
我已经有运行代码,可以进行撤消操作,但是我无法在CTRL-Z上触发它,因为使用“ Z”键快捷方式已被保留。 For example, if I call my own undo with
event.key()==(Qt.Key_Control and Qt.Key_Y)
, it works. 例如,如果我使用
event.key()==(Qt.Key_Control and Qt.Key_Y)
调用自己的撤消,则可以正常工作。
In C++ you would have to install an event filter, probably with PyQt this is similar (overwrite virtual bool eventFilter(QObject* pObject, QEvent* pEvent); in your editor class). 在C ++中,您必须安装一个事件过滤器,可能与PyQt相似(覆盖虚拟布尔eventFilter(QObject * pObject,QEvent * pEvent);在您的编辑器类中)。 CTRL-Z gets probably filtered by the QTextEdit event filter thus it never gets to keyPressEvent.
CTRL-Z可能被QTextEdit事件过滤器过滤,因此它永远不会到达keyPressEvent。
Ah, So additionally to the keyPressEvent which I have in my second class, you have to put it also into the Subclass! 嗯,所以除了我在第二堂课中拥有的keyPressEvent之外,还必须将它也放入子类中!
class Q_My_TextEdit(QtGui.QTextEdit):
def __init__(self, *args):
QtGui.QTextEdit.__init__(self, *args)
def keyPressEvent(self,event):
if event.key()==(Qt.Key_Control and Qt.Key_Z):
self.undo()
#To get the remaining functionality back (e.g. without this, typing would not work):
QTextEdit.keyPressEvent(self,event)
def undo(self):
print("undo")
#my own undo code
But now I can't type into my textedit any more! 但是现在我不能再输入textedit了! How can I solve this?
我该如何解决?
--> Solved . -> 解决了 。 See Properly handling a keyPressEvent in a Subclassed PyQT LineEdit
请参见在子类化PyQT LineEdit中正确处理keyPressEvent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.