Pressing Command + Backspace on MacOs normally deletes the current line.
Is it possible to reproduce this behaviour in QPlainTextEdit
? It works as expected with QLineEdit
.
Here's a minimal example to reproduce the issue:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QKeySequence
app = QApplication([])
text = QPlainTextEdit()
window = QMainWindow()
window.setCentralWidget(text)
window.show()
app.exec_()
I am running the following: Python 3.6.10 PyQt5 5.14.1 MacOS 10.14.6
You should probably subclass QPlainTextEdit and override its keyPressEvent.
As far as I can understand, on MacOS command+backspace deletes the text at the left of the current cursor position, but it's also possible to delete the entire line, no matter what.
In any case:
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)