Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QTextEdit:在PyQt4中使用undo()覆盖Ctrl-Z_Python_Qt_Pyqt_Undo - Fatal编程技术网

Python QTextEdit:在PyQt4中使用undo()覆盖Ctrl-Z

Python QTextEdit:在PyQt4中使用undo()覆盖Ctrl-Z,python,qt,pyqt,undo,Python,Qt,Pyqt,Undo,我将qtexdit子类化 class Q_My_TextEdit(QtGui.QTextEdit): def __init__(self, *args): QtGui.QTextEdit.__init__(self, *args) def undo(self): print("undo") #my own undo code 在我的另一节课上: self.textedit=Q_My_TextEdit() def keyPres

我将qtexdit子类化

class Q_My_TextEdit(QtGui.QTextEdit):
    def __init__(self, *args):
        QtGui.QTextEdit.__init__(self, *args)

    def undo(self):
        print("undo")
        #my own undo code
在我的另一节课上:

self.textedit=Q_My_TextEdit()

def keyPressEvent(self,event):
    if event.key()==(Qt.Key_Control and Qt.Key_Z):
        self.textedit.undo()
如果在QTextEdit中键入一些文本,并按CTRL-Z,它将被撤消,但不调用函数“撤消”。那么它是如何详细工作的呢


背景是,在第二步中,我想设置新文本(setText()),这样就删除了撤销堆栈。我已经有了执行撤销的代码,但是我不能在CTRL-Z上触发它,因为使用“Z”键时,keysthortcut是以某种方式保留的。例如,如果我使用
event.key()==(Qt.key\u Control和Qt.key\u Y)
调用我自己的撤销,它就会工作< P> >您必须安装事件过滤器,可能与PyQT类似(覆盖虚拟BooEnter Flass(QObjtPoBoStudio,QEvt*pEvter);在编辑器类中)。CTRL-Z可能会被QTextEdit事件过滤器过滤,因此它永远不会进入keyPressEvent。

啊,所以除了我在第二个类中的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
但是现在我再也不能在文本编辑中输入了!我怎样才能解决这个问题


-->已解决。请参见

啊,除了我在第二个类中使用的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
但是现在我再也不能在文本编辑中输入了!我怎样才能解决这个问题


-->已解决。请参见

谢谢您的回答!谢谢你的回答!