QAction from QToolBar focus behaviour

  • Hi,

    Currently I am working on an interface using QAction items from a QToolBar. I understand that QAction objects are not QWidget elements, although they get a "button" representation when I add them into my toolbar.

    Now, when I click on any QAction item icon (in this context), it seems that the focus is captured by that item. As the QAction is not a button, not even a QWidget, how can I release the focus from it?

    Thanks for your help.

    PS: I tried using setChecked() and toggle() methods with no luck.

  • Lifetime Qt Champion


    Maybe clearFocus ?

    Hope it helps

  • clearFocus() is not available for the QAction class so I used it with the QToolBar object with no luck.

    The effect I am trying to avoid is very curious. I noticed that the focus/checked mark only appears when I click any QAction for the first time, then a subtle square remains on them. If I click on the toolbar or any other place, the mark around the QAction buttons used doesn't disappear, therefore it shouldn't be a focus reference.

    Here is a low resolution example. Pay attention to the Cancel and the Gear icons "focus" effect. The blue button doesn't have it.
    alt QToolBar Buttons

  • Moderators

    regarding the checkmark issue are you using any stylesheet style?

    Try also this:

    foreach( QToolButton* btn, toolBar->findChildren<QToolButton*>() )
        btn->setFocusPolicy( Qt::NoFocus );

  • @raven-worx
    Well, in fact I set a basic stylesheet definition to the toolbar just to redefine the background color. I will remove it temporarily just to find out if that is the cause of the problem.

    Thank you for your suggestion, I will give it a try.

  • Ok, I tried removing the stylesheet instruction and it didn't work. The foreach instruction neither made any effect.
    I know this is a very subtle issue. I can live with it, but it's more about curiosity... I mean, what is the cause of this behaviour?

    Anyway, thank you for the support.

    PS: I am testing this code from an Android tablet. Maybe is this a collateral issue of using QWidgets in Android?

  • Got the same problem, i.e. Android icons stay "highlighted" on QToolbar. I tried many things, include playing with QAndroidStyle or filtering events. What finally worked was to generate Leave and HoverLeave events whenever an action is triggered.

    So I connect all my actions to this slot:

    void ToolBar::slot_unfocusAction()
        QAction * action = qobject_cast<QAction*>(sender());
        foreach(QWidget * wid, action->associatedWidgets()) {
            QMouseEvent event1(QEvent::Leave, QPointF(0.0, 0.0), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
            QApplication::sendEvent(wid, &event1);
            QMouseEvent event2(QEvent::HoverLeave, QPointF(0.0, 0.0), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
            QApplication::sendEvent(wid, &event2);

    I hope this will help someone.

  • @maitai_vw ,

    Instead of adding actions to toolbar, You can add the toolbuttons to toolbar, then you can call clearfocus for those toolbuttons.

Log in to reply

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