I'm attempting to write a small command line console in PyQt, similar to the Jupyter Qtconsole . As a simple first step I'd like to be able to execute commands when the key combination 'Shift+Return' is pressed. The code example below suggests there's a specific interaction between 'Shift+Return' on QPlainTextEdit so that it behaves differently from a random different shortcut, like 'Ctrl+k'. With 'Shift+Return' the below code does not print "Bar" when 'Shift+Return' is pressed, whereas 'Ctrl+k' does print "Foo".
The questions QShortcut & QKeySequence with Shift+Return in a QPlainTextEdit element is similar, and proposes a solution and QPlainTextEdit - change shift+return behaviour both are about the same topic, and propose similar solutions. But I'm curious why this is even a problem in the first place. Why doesn't 'Shirt+Return' work with the QPlainTextEdit?
import PyQt6.QtWidgets as qw
import PyQt6.QtGui as qg
import PyQt6.QtCore as qc
class Console(qw.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
self.qptext = qw.QPlainTextEdit()
self.shortcut1 = qg.QShortcut(
qg.QKeySequence('Ctrl+k'),
self)
self.shortcut1.activated.connect(lambda : print("Foo"))
self.shortcut2 = qg.QShortcut(
qg.QKeySequence('Shift+Return'),
self)
self.shortcut2.activated.connect(lambda : print("Bar"))
self.setCentralWidget(self.qptext)
app = qw.QApplication([])
window = Console()
window.show()
app.exec()