Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Find which widget had focus when menu item is clicked?



  • After clicking on an item in the application menu, Qt automatically restores focus back to the last active widget. So, for example, if you had a QLineEdit in focus, then click on a menu item, the QLineEdit that was previously in focus will get focus restored after clicking the menu item. It therefore appears that Qt keeps track of the last widget that had focus before a menu item was clicked, and I'm looking for a way to find this widget.

    Is there a function to get which widget had focus before a menu action was performed?



  • @Elsworth55 ,Hi,friends, welcome.

    I found these infomation in qt manual.

    [static] QWidget *QApplication::focusWidget()
    Returns the application widget that has the keyboard input focus, or 0 if no widget in this application has the focus.
    See also QWidget::setFocus(), QWidget::hasFocus(), activeWindow(), and focusChanged().
    

    Try it, maybe it will help you.



  • Thanks for the suggestion. I had tried that function, but when you click on a menu item it gives focus to the menu, so the focusWidget() will be the menu. This therefore can't be used to find out which widget had focus before clicking the menu item.

    The best solution I've come up with is to use the QLineEdit editingFinished() signal and store which QLineEdit last had focus. However, this gets a bit messy due to various reasons, so I was hoping there might just be a function to get the last active widget, but so far I've been unable to find one.


  • Moderators

    @Elsworth55
    This is the function you're looking for.

    QWidget *QWidget::previousInFocusChain() const
    alt text


  • Qt Champions 2019

    @J.Hilk said in Find which widget had focus when menu item is clicked?:

    QWidget::previousInFocusChain

    I don't think this is what he needs. This one returns the widget which is before the current in the focus chain. It does not have anything to do with the focus history. It is about the focus order (which widget will get focus if you press TAB key).


  • Moderators

    @jsulm said in Find which widget had focus when menu item is clicked?:

    @J.Hilk said in Find which widget had focus when menu item is clicked?:

    QWidget::previousInFocusChain

    I don't think this is what he needs. This one returns the widget which is before the current in the focus chain. It does not have anything to do with the focus history. It is about the focus order (which widget will get focus if you press TAB key).

    oh, seems like my jedi skills are rusty ...



  • Thanks to everyone for the suggestions. I thought I might be overlooking something, but it looks like there is no function to get the last widget in focus.

    I'll just use the editingFinished() signal and store the last QLineEdit that had focus. It's a bit more messy that way, but it should work fine.

    Thanks again.



  • Heads-up for anyone reading this. I have come across this topic in trying to solve my new post at https://forum.qt.io/topic/112224/signal-slot-vs-direct-call-and-related-question, where I need to know which window/widget has the focus when a (main window) menu is invoked.

    The OP (@Elsworth55 )'s question here seems "alarmist"! Of course we need Qt to tell us which widget is focussed when a menu is invoked, else we're in trouble! Maybe it never got an answer because I do not find the behaviour reported by the OP here.

    Tested under Qt 5.12.2, Linux. I find that QApplication.focusWidget() does return the correct, currently-focussed widget. I have tested this on both a generic QWidget and a QLineEdit, and I have tested both when the menu is pulled down (QMenu::aboutToShow()) and when a item is clicked (QAction::triggered). In all cases I found it worked, so I don';t know about this OP's

    the QLineEdit that was previously in focus will get focus restored after clicking the menu item
    but when you click on a menu item it gives focus to the menu, so the focusWidget() will be the menu


Log in to reply