Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find which widget had focus when menu item is clicked?
Forum Updated to NodeBB v4.3 + New Features

Find which widget had focus when menu item is clicked?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 13.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elsworth55
    wrote on last edited by
    #1

    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?

    joeQJ 1 Reply Last reply
    0
    • E Elsworth55

      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?

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

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

      Just do it!

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Elsworth55
        wrote on last edited by
        #3

        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.

        J.HilkJ 1 Reply Last reply
        0
        • E Elsworth55

          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.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

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

          QWidget *QWidget::previousInFocusChain() const
          alt text


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          jsulmJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

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

            QWidget *QWidget::previousInFocusChain() const
            alt text

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @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).

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            J.HilkJ 1 Reply Last reply
            0
            • jsulmJ jsulm

              @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).

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

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


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • E Offline
                E Offline
                Elsworth55
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  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

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved