Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, April 2007
Getting hold of focus events from "designed" components


Message 1 in thread

Hello!

I've been using the designer to construct a widget with an editable
combo box in it. I want to display an explaining text in the editable
field which I want to disappear when the user gives focus to this
component. However, I cannot seem to find a way to catch these focus
events. Standard way is to override the focusInEvent of the widget, but
this cannot be done in the generated code since it will be overwritten
the next time I run the generator.

Do I have to use some global event catching here to be able to get hold
of such focus events? What is the general recommended way to catch these
"override" events for components that cannot be overridden. It would be
nice if some sort of adapter could be installed into these components to
handle this...

Best regards,
Helge Fredriksen


Message 2 in thread

Helge Fredriksen wrote:
> Hello!
> 
> I've been using the designer to construct a widget with an editable
> combo box in it. I want to display an explaining text in the editable
> field which I want to disappear when the user gives focus to this
> component. However, I cannot seem to find a way to catch these focus
> events. Standard way is to override the focusInEvent of the widget, but
> this cannot be done in the generated code since it will be overwritten
> the next time I run the generator.
> 
> Do I have to use some global event catching here to be able to get hold
> of such focus events? What is the general recommended way to catch these
> "override" events for components that cannot be overridden. It would be
> nice if some sort of adapter could be installed into these components to
> handle this...

Hi Helge,

You can always intercept events before they are sent to the component by 
installing an event filter on that component. See:

http://doc.trolltech.com/qtjambi-1.0/com/trolltech/qt/core/QObject.html#installEventFilter(com.trolltech.qt.core.QObject)

Here you could install the event filter on the component with the 
special focus behaviour and then.

An alternative approach is to subclass QLineEdit like this:

public class YourLineEdit extends QLineEdit {
     public Signal0 focusIn = new Signal0();
     protected void focusEvent(QFocusEvent e) {
         if (e.gotFocus())
             focusIn.emit();
     }
}

And expose YourLineEdit as a custom widget in designer. Then you can 
connect to the focusIn signal in your code.

best regards,
Gunnar