[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();

    ..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?


  • Moderators

    YourClass *focused_widget;

    focused_widget = q_object_cast<YourClass>(QWidget::focusWidget());

  • Seems not to be possible in assignment, so tried


    .. 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());

Log in to reply

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