[Solved] Use of focusWidget()



  • Hi

    I am trying to find which of the buttons has the focus and do an animateClick() on it.

    @QWidget *focused_widget;

    focused_widget = QWidget::focusWidget();
    focused_widget->animateClick(ANIMATE_CLICK_TIME);@

    ..does not work and returns: 'class QWidget' has no member named 'animateClick'
    Can I somehow casting the widget type or how do I solve this?

    Thanks


  • Moderators

    @
    YourClass *focused_widget;

    focused_widget = q_object_cast<YourClass>(QWidget::focusWidget());
    focused_widget->animateClick(ANIMATE_CLICK_TIME);
    @



  • Seems not to be possible in assignment, so tried

    @qobject_cast<AsButton>(focused_widget)->animateClick(ANIMATE_CLICK_TIME);@

    .. still no luck ... I am still doing something wrong


  • Moderators

    Waaait, QWidget::focusWidget() is not a static method! You need to run it either on your top-level widget, or go with QApplication::focusWidget(). And of course, I forgot to add a pointer in my cast:
    @
    focused_widget = q_object_cast<YourClass *>(QWidget::focusWidget());
    @



  • Thanks a lot.
    Final solution which works perfect:

    @AsButton *focused_widget;

    focused_widget = qobject_cast<AsButton *>(QApplication::focusWidget());
    focused_widget->animateClick(ANIMATE_CLICK_TIME);@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.