Daniel Segal
Daniel Segal

Reputation: 117

How can I know when the Enter key was pressed on QTextEdit

I'm writing Chat gui for client on Python using PyQt5. I have a QTextEdit, which the client can write messages in it. I wan't to know when the 'Enter' key is being pressed while the focus is on the QTextEdit.

I tried using installEventFilter function but it detects keys being pressed on all of the other widgets but the QTextEdit one. What can I do to fix that?

def initUI(self):
   # ...
    self.text_box = QtWidgets.QTextEdit(self)
    self.installEventFilter(self)
    # ...

def keyPressEvent(self, qKeyEvent):
    print(qKeyEvent.key())
    if qKeyEvent.key() == Qt.Key_Return:
        if self.text_box.hasFocus():
            print('Enter pressed')

Upvotes: 6

Views: 12430

Answers (4)

抹茶抹茶
抹茶抹茶

Reputation: 31

For those who use PyQt6, here is the code:

import PyQt6.QtCore
import PyQt6.QtWidgets

def initUI(self):
    # ...
    self.text_box = QtWidgets.QTextEdit(self)
    self.text_box.installEventFilter(self)
    # ...

def eventFilter(self, obj, event):
    if event.type() == QtCore.QEvent.Type.KeyPress and obj is self.text_box:
        if event.key() == QtCore.Qt.Key.Key_Return and self.text_box.hasFocus():
            print('Enter pressed')
    return super().eventFilter(obj, event)

QtCore.QEvent.KeyPress was changed to QtCore.QEvent.Type.KeyPress, and QtCore.Qt.Key_Return was changed to QtCore.Qt.Key.Key_Return.

Upvotes: 0

tbrodbeck
tbrodbeck

Reputation: 528

When you override keyPressEvent you are listening to the events of the window, instead install an eventFilter to the QTextEdit, not to the window as you have done in your code, and check if the object passed as an argument is the QTextEdit:

def initUI(self):
    # ...
    self.text_box = QtWidgets.QTextEdit(self)
    self.text_box.installEventFilter(self)
    # ...

def eventFilter(self, obj, event):
    if event.type() == QtCore.QEvent.KeyPress and obj is self.text_box:
        if event.key() == QtCore.Qt.Key_Return and self.text_box.hasFocus():
            print('Enter pressed')
            return True
    return False

This is building upon the answer of @eyllanesc and the problem @Daniel Segal faced. Adding the correct return values as such to the eventFilter solves the problem.

Upvotes: 2

bfris
bfris

Reputation: 5805

The answer from @eyllanesc is very good if you are determined to use QTextEdit.

If you can get away with QLineEdit and its limitations, you can use the returnPressed() signal. The biggest drawback for QLineEdit is you are limited to one line of text. And there is no word wrap. But the advantage is you don't have to mess with eventFilters or think too hard about how keyPress signals fall through all of the widgets in your app.

Here is a minimal example that copies from one QLineEdit to another:

import sys

from PyQt5.QtWidgets import * 


class PrintWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.left=50
        self.top=50
        self.width=300
        self.height=300
        self.initUI()

    def initUI(self):

        self.setGeometry(self.left,self.top,self.width,self.height)

        self.line_edit1 = QLineEdit(self)
        self.line_edit1.move(50, 50)
        self.line_edit1.returnPressed.connect(self.on_line_edit1_returnPressed)

        self.line_edit2 = QLineEdit(self)
        self.line_edit2.move(50, 100)

        self.show()

    def on_line_edit1_returnPressed(self):
        self.line_edit2.setText(self.line_edit1.text())

if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = PrintWindow()
    sys.exit(app.exec_())

In this example, I have manually connected to the signal in line 22 (self.line_edit1.returnPressed.connect). If you are using a ui file, this connection can be left out and your program will automatically call the on__returnPressed method.

Upvotes: 3

eyllanesc
eyllanesc

Reputation: 243887

When you override keyPressEvent you are listening to the events of the window, instead install an eventFilter to the QTextEdit, not to the window as you have done in your code, and check if the object passed as an argument is the QTextEdit:

def initUI(self):
    # ...
    self.text_box = QtWidgets.QTextEdit(self)
    self.text_box.installEventFilter(self)
    # ...

def eventFilter(self, obj, event):
    if event.type() == QtCore.QEvent.KeyPress and obj is self.text_box:
        if event.key() == QtCore.Qt.Key_Return and self.text_box.hasFocus():
            print('Enter pressed')
    return super().eventFilter(obj, event)

Upvotes: 10

Related Questions