Discussion:
handling Enter in a qtextedit?
inhahe
2009-09-08 05:55:40 UTC
Permalink
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.
Lukas Hetzenecker
2009-09-08 10:19:50 UTC
Permalink
Hello,
Post by 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.
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 <***@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
Post by 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.
Lukas Hetzenecker
2009-09-08 13:35:04 UTC
Permalink
Hello,

i attatched a small example, if you need more informations just mail me.
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.
Post by Lukas Hetzenecker
Hello,
Post by 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
Post by inhahe
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 )
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 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
super(MessageTextEdit, self).__init__(parent)
self.parent = parent
self.__sendMessageOnReturn = False
return self.__sendMessageOnReturn
self.__sendMessageOnReturn = state
event = QKeyEvent(QEvent.KeyPress, Qt.Key_Return,
Qt.NoModifier)
self.emit(SIGNAL("sendMessage"))
return
QTextEdit.keyPressEvent(self, event)
Lukas
Post by 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
Post by inhahe
key presses.
i guess that means if i want to react to a user pressing Enter they're
not
Post by inhahe
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.
Post by inhahe
thanks.
Loading...