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

荒凉一梦 提交于 2020-06-23 11:01:53

问题


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')

回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/57698744/how-can-i-know-when-the-enter-key-was-pressed-on-qtextedit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!