[PyQt] QLineEdit and focusOutEvent problem

Greg Smith gsmith at troublemakerstudios.com
Tue Apr 21 20:03:23 BST 2009


Well I found the solution, and it was simple enough to uses the
editingFinished() signal, as this was exactly what I was looking for.

 

Greg

 

________________________________

From: pyqt-bounces at riverbankcomputing.com
[mailto:pyqt-bounces at riverbankcomputing.com] On Behalf Of Greg Smith
Sent: Monday, April 20, 2009 7:55 PM
To: pyqt at riverbankcomputing.com
Subject: [PyQt] QLineEdit and focusOutEvent problem

 

Hello there, I'm new to the mailing list as well as PyQt in general, so
please bare with me, and I apologize ahead of time for questions that
may be trivial. So far I dig PyQt, I've been very pleased with
everything, that is until I had my encounter with events. Maybe its lack
of knowledge when it comes to event handling paradigm, but it just seems
like plain voodoo to me. 

 

The current tool I am working on uses a QDialog as the main widget. Its
being called as a dialog box for a custom plugin I have written for Nuke
(compositing software). One of the behaviors I am trying to incorporate
in several QLineEdit widgets is to execute the same method used on a
returnPressed() Signal, or execute the signal itself. But I am getting
ahead of myself here, executing logic is not the problem, its residue
behavior that occurs when I either try to override a widgets
focusOutEvent() or define an eventFilter for my object. 

 

I first used an event filter; this seemed like the most logical solution
for what I needed, However when I setup the event trigger, it looked
like the widget not losing focus properly. After I focused out by
clicking on another widget, a text cursor would remain flashing in the
widget. Also it didn't seem to update the stylesheet settings either. I
have a my stylesheet assigning a different color border for whichever
widget has focus. With the event filter, when a widget loses focus, the
border remains highlighted unless I mouse over it.

 

            ...in the __init__() method

self.ui.pth_ln.installEventFilter(self)

            self.ui.desc_ln.installEventFilter(self)

            self.ui.vrsn_ln.installEventFilter(self)

 

def eventFilter(self, o, e):

if o == self.ui.pth_ln:

                if e.type() == QtCore.QEvent.FocusOut:

                    utils.executeInMainThread(nuke.tprint, ('path out
focus',))

                    self.ui.pth_ln.clearFocus()

                    return True

                else:

                    return False

            if o == self.ui.desc_ln:

                if e.type() == QtCore.QEvent.FocusOut:

                    utils.executeInMainThread(nuke.tprint, ('description
out focus',))

                    self.ui.desc_ln.clearFocus()

                    return True

                else:

                    return False

            if o == self.ui.vrsn_ln:

                if e.type() == QtCore.QEvent.FocusOut:

                    utils.executeInMainThread(nuke.tprint, ('version out
focus',))

                    self.ui.vrsn_ln.clearFocus()

                    return True

                else:

                    return False

            #else:

            return self.eventFilter(o,e)

 

In place of the methods I want to execute I used nukes terminal print
method.

 

My second try was to override the widget's focusOutEvent. Here is what I
did 

 

In my __init__() method, I have:

            self.ui.pth_ln.focusOutEvent = self.path_focusOutEvent

            self.ui.desc_ln.focusOutEvent = self.desc_focusOutEvent

            self.ui.vrsn_ln.focusOutEvent = self.vrsn_focusOutEvent

 

and the corresponding methods:

        def pth_FocusOutEvent(self, event):

            utils.executeInMainThread(nuke.tprint, ('path off focus',))

            self.ui.pth_ln.clearFocus()

            event.accept()

 

        def desc_FocusOutEvent(self, event):

            utils.executeInMainThread(nuke.tprint, ('description off
focus',))

            self.ui.desc_ln.clearFocus()

            event.accept()

 

        def vrsn_FocusOutEvent(self, event):

            utils.executeInMainThread(nuke.tprint, ('version off
focus',))

            self.ui.vrsn_ln.clearFocus()

            event.accept()

 

Again, same behavior as found in the event filters. So it seems like
when the default event is getting overridden, I am not introducing back
code that enables the behavior I am after, As if the widget is not being
fully released, that still thinks has focus even though the keyboard has
been released from the widget. So, I don't know. I am hoping someone can
enlighten me on how to get it to work, or maybe even suggest a work
around that doesn't use events but signals instead, I seem to have
better luck with those.

 

Thanks, 

 

Greg

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090421/5649e3b6/attachment-0001.html


More information about the PyQt mailing list