PyQt QPlainTextEdit "Command + Backpace" 不会删除 MacOS 上的行



在MacO上按命令+Backspace通常会删除当前行。有可能在QPlainTextEdit中重现这种行为吗?它与QLineEdit一起正常工作。

这里有一个最小的例子来重现这个问题:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QKeySequence

app = QApplication([])
text = QPlainTextEdit()
window = QMainWindow()
window.setCentralWidget(text)
window.show()
app.exec_()

我正在运行以下内容:Python 3.6.10PyQt5 5.14.1MacOS 10.14.6

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

据我所知,在MacOS命令+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)

最新更新