[Qt-interest] Determining Whether Focus is in the Combobox DropDown
Girish Ramakrishnan
girish at forwardbias.in
Thu Oct 7 21:57:26 CEST 2010
Hi,
On Wed, Oct 6, 2010 at 7:34 PM, Jake Colman <colman at ppllc.com> wrote:
>
> I am porting an application from Qt3 to Qt4. I have an event filter
> that traps FocusOut events for a specific widget. That event filter
> needs to know whether the FocusOut event have moved focus into the
> combobox's dropdown menu.
>
> In Qt3 I can do the following:
>
> if( Event->type() == QEvent::FocusOut )
> {
> QWidget* EditorWidget = cellWidget( currentRow(), currentColumn() );
> if( EditorWidget && EditorWidget == Obj )
> {
> QWidget* FocusWidget = qApp->focusWidget();
> // If the FocusWidget is NULL, then focus is on a non-Qt widget and we
> // should endEdit. Otherwise, we endEdit only if the FocusWidget is
> // not a child of the EditorWidget (which would make it a pop-up menu)
> // and not the combox drop-down list box.
> if( !FocusWidget ||
> QString(FocusWidget->name()) != static_cast<const
> char*>("in-combo") )
> {
> endEdit( currentRow(), currentColumn(), true, false );
> return true;
> }
> }
> }
>
> The above code relies on the fact that when the combobox dropdown is
> opened, focus is on a widget named "in-combo". If we are not focused on
> the "in-combo" widget, then we endEdit().
>
> Since the combobox has been dramatically rewritten for Qt4, this code
> does not work - even when using the Q3ComboBox since it is just a
> wrapper around the revised underlying implementation.
>
> Does anyone have any suggestions on how I can port this code?
>
Here's how it's implemented - The QComboBox's popup is a QAbstractItemView
(see QComboBox::view()). The popup's parent is an internal widget but this
widget widget will have the Qt::Popup window flag. The parent of this
internal widget is the QComboBox itself. Of course, all this is internal and
you should not rely on it :-) So, in your code, you can qobject_cast to
QAbstractItemView and check if it's parentWidget()'s flag has Qt::Popup and
popup->parentWidget()->parentWidget() is a QComboBox.
Girish
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101008/167695a0/attachment.html
More information about the Qt-interest-old
mailing list