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 <[email protected]> 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 <[email protected]>
> >
> > 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.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class Widget(QWidget):
    def __init__(self, parent=None):
       super(Widget, self).__init__(parent)
       
       # Create a Text edit widget and lay it out...

       self.textedit = QTextEdit()
       self.layout = QVBoxLayout(self)
       self.layout.addWidget(self.textedit)

       self.setLayout(self.layout)

       # Install an event filter for the QTextEdit
       self.textedit.installEventFilter(self)  # all events will call self.eventFilter(self.textedit, event)

    def eventFilter(self, obj, ev): # http://qt.nokia.com/doc/4.5/qobject.html#eventFilter
       if ev.type() == QEvent.KeyPress: # List of events: http://qt.nokia.com/doc/4.5/qevent.html#Type-enum
          print "Pressed key: " + str(ev.key()) + " in widget " + str(obj)
          
          # If we return True, it's not possible to enter text in the TextEdit.
          # It's possible to check if event.key() == Qt.Key_Return and then return True and emit a signal, otherwise return False

          # From the documentation:
          # In your reimplementation of this function, if you want to filter the 
          # event out, i.e. stop it being handled further, return true; otherwise return false.
          return True 
 
       QWidget.eventFilter(self,  obj,  ev)
       return False 

app = QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to