[PyKDE] Newbie question on QWidget.focusOutEvent

Hans-Peter Jansen hpj at urpla.net
Sat Sep 24 18:13:24 BST 2005


[I'm CCing PyKDE to make it available for others too]

Am Samstag, 24. September 2005 14:31 schrieb Volker Lenhardt:
> Hi Pete,
>
> leider haben die Listenmoderatoren meine jeweiligen Antworten noch
> nicht eingestellt, so dass ich auch deine Antwort nicht als Mail
> erhalten habe. Ich vermute, sie ist bei Giovanni gelandet.

I can highly encourage you to subscribe, since this list is low traffic 
and has an unusual high signal/noise ratio on PyQt, PyKDE and 
friends ;-)

>  > If that is not your problem, it could be related to an Qt issue
>  > with container widgets, where the container widget itself will
>  > never receive
>  > certain events, but the embedded widgets (QEditLine) in this case.
>  > A workaround is to install an event filter on those widgets (and
>  > on the
>  > possibly involved proxy widgets), and handle the events, you need
>  > in the eventFilter() method.
>  >
>  > If you cannot follow me, just call back, and I will post some code
>  > fragments.
>  >
>  > Pete
>
> Thank you Pete. Sure I'm interested. And equally sure I can't follow
> you. But as I'm using Qtopia, an embedded system, you certainly are
> on the right track.
>
> Das war meine bisher noch unveröffentlichte Antwort. In der
> Zwischenzeit ist mir eine Ahnung der Zusammenhänge gekommen und ich
> habe
> festgestellt, dass das focusOutEvent durchkommt, wenn ich die
> Combobox auf nicht editierbar stelle. Damit scheint mir sicher zu
> sein, dass die editierbare Combobox QLineEdit benutzt, wie du es
> beschrieben hast.

QComboBox even provides a method editLine() to get to it.

This goes typically into the constructor of your dialog implementation 
(sub)class: 

        for w in (self.combo1, self.combo2):
            w.installEventFilter(self)

Special event handling goes here:

    # special return/enter key event handling
    def eventFilter(self, obj, e):
        if e and obj:
            if e.type() in (QEvent.KeyPress, QEvent.KeyRelease):
                if e.key() in (Qt.Key_Return, Qt.Key_Enter):
                    if obj == self.combo1 and self.combo1valid():
                        self.combo2.setFocus()
                        self.combo2.lineEdit().home(False)
                    elif obj == self.combo2 and self.combo2valid():
                        self.editline1.setFocus()
                        self.editline1.home(False)
                    return True
        return False

Here I do something quite different, but it should get you going.

>
> Ich bin sehr gespannt auf den Lösungsansatz.

Easy, isn't it? If you're going to use container widgets, which itself 
use focus proxy widgets (e.g. QSpinBox), then you install the event 
handler on them instead:

        for w in (self.combo1, self.combo2):
            fp = w.focusProxy()
            if fp:
                fp.installEventFilter(self)
            else:
                w.installEventFilter(self)


> Vielen Dank schon mal.
You're welcome,

Call back, if you stuck again..

Cheers,
Pete




More information about the PyQt mailing list