Python PyQt QPlainTextEdit“;命令+;“后退”;不';不要删除MacOS上的行

Python PyQt QPlainTextEdit“;命令+;“后退”;不';不要删除MacOS上的行,python,pyqt,pyqt5,qplaintextedit,Python,Pyqt,Pyqt5,Qplaintextedit,在MacOs上按Command+Backspace通常会删除当前行。 是否可以在QPlainTextEdit中重现此行为?它在QLineEdit中正常工作 这里有一个简单的例子来重现这个问题: 从PyQt5.QtWidgets导入* 从PyQt5.QtGui导入QKeySequence app=QApplication([]) text=QPlainTextEdit() window=qmainfown() window.setCentralWidget(文本) window.show() ap

在MacOs上按Command+Backspace通常会删除当前行。 是否可以在
QPlainTextEdit
中重现此行为?它在
QLineEdit
中正常工作

这里有一个简单的例子来重现这个问题:

从PyQt5.QtWidgets导入*
从PyQt5.QtGui导入QKeySequence
app=QApplication([])
text=QPlainTextEdit()
window=qmainfown()
window.setCentralWidget(文本)
window.show()
app.exec()
我正在运行以下程序: Python 3.6.10 PyQt5.14.1
MacOS 10.14.6

您可能应该将QPlainTextEdit子类化并覆盖其keyPressEvent

据我所知,在MacOS上,command+backspace会删除当前光标位置左侧的文本,但也可以删除整行内容,无论发生什么情况

无论如何:

class PlainText(QPlainTextEdit):
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Backspace and event.modifiers() == Qt.ControlModifier:
            cursor = self.textCursor()

            # use this to remove everything at the left of the cursor:
            cursor.movePosition(cursor.StartOfLine, cursor.KeepAnchor)
            # OR THIS to remove the whole line
            cursor.select(cursor.LineUnderCursor)

            cursor.removeSelectedText()
            event.setAccepted(True)
        else:
            super().keyPressEvent(event)

这管用!我不得不将if语句更改为
event.nativeModifiers()==1048840
think。