[PyQt] handling Enter in a qtextedit?

Lukas Hetzenecker LuHe at gmx.at
Tue Sep 8 11:19:50 BST 2009


Hello,

> The documentation for keyPressEvent for a QTextEdit says
>
> This function is called with key event *e* when key presses occur. It
> handles PageUp, PageDown, Up, Down, Left, and Right, and ignores all other
> key presses.

Hm, i don't know why the documentation says this, but it works for me. Maybe 
the implemention for QWidget is used 
(http://qt.nokia.com/doc/4.5/qwidget.html#keyPressEvent  )

I did the same in my project:
http://series60-remote.svn.sf.net/viewvc/series60-
remote/trunk/pc/widget/MessageTextEdit.py?view=markup

You can replace self.emit(SIGNAL("sendMessage")) by self.clear().

I subclassed QTextEdit and overwrote the keyPressEvent
It's even possible to include this widget in Qt Designer - do you need an 
example for this?

An alternative would be to install an event filter on your QWidget (Dialog, 
MainWindow or whatever...), so you don't have to subclass QTextEdit and you 
get the events for the text edit too - I could provide you an example if you 
want.

http://series60-remote.svn.sf.net/viewvc/series60-
remote/trunk/pc/widget/MessageTextEdit.py?view=markup

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2008 - 2009 Lukas Hetzenecker <LuHe at gmx.at>

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MessageTextEdit(QTextEdit):
    def __init__(self,  parent):
        super(MessageTextEdit,  self).__init__(parent)

        self.parent = parent
        self.__sendMessageOnReturn = False
    
    def sendMessageOnreturn(self):
        return self.__sendMessageOnReturn
    
    def setSendMessageOnReturn(self,  state):
        self.__sendMessageOnReturn = state

    def keyPressEvent(self,  event):
        if self.__sendMessageOnReturn:
            if event.key() == Qt.Key_Return:
                if event.modifiers() == Qt.ControlModifier:
                    event = QKeyEvent(QEvent.KeyPress,  Qt.Key_Return,  
Qt.NoModifier)
                else:
                    self.emit(SIGNAL("sendMessage"))
                    return

        QTextEdit.keyPressEvent(self,  event)


Lukas

Am Dienstag 08 September 2009 07:55:40 schrieb inhahe:
> The documentation for keyPressEvent for a QTextEdit says
>
> This function is called with key event *e* when key presses occur. It
> handles PageUp, PageDown, Up, Down, Left, and Right, and ignores all other
> key presses.
> i guess that means if i want to react to a user pressing Enter they're not
> making it easy for me?
> i thought maybe i could handle the textChanged event and then grab the
> current state of the keyboard to see if Enter is pressed, but I don't
> even see a function to check the keyboard state.
> so what's the best way to do this?
> do I have to use QAction in some way?
>
> what i want to do specifically is create an edit box where pressing
> shift+enter works like pressing enter but pressing enter alone calls a
> function which clears the text, etc. just like how an AIM input box works.
>
> thanks.


More information about the PyQt mailing list