[PyQt] handling Enter in a qtextedit?

Lukas Hetzenecker LuHe at gmx.at
Tue Sep 8 14:35:04 BST 2009


Hello,

i attatched a small example, if you need more informations just mail me. 

Am Dienstag 08 September 2009 13:03:01 schrieben Sie:
> hi, thanks!i'm interested in what you said about the event filter. i don't
> like having to subclass some things while for other things i can just use
> signals.  i'm thinking that depending on how this "event filter" thing
> works, maybe i could make a convenience function using it that will allow
> me to treat any event handler like a signal..!

Yes, that should be possible (havn't tried it myself).
You only have to install a event filter for all widgets you want to get the 
events and emit a signal in the eventFilter method.

>
> On Tue, Sep 8, 2009 at 6:19 AM, Lukas Hetzenecker <LuHe at gmx.at> wrote:
> > 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: eventfilter.py
Type: text/x-python
Size: 1431 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090908/9568d57e/eventfilter.py


More information about the PyQt mailing list