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. [SOLVED] How to detect QStackedWidget focus?

[SOLVED] How to detect QStackedWidget focus?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.2k Views 1 Watching
  • 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    I have a QStackedWidget and I need to know when it receives focus (in other words, when any of its child widgets receives focus). I've tried installing an event filter onto it:

    @bool CMainWindow::eventFilter(QEvent * e, QObject* o)
    {
    if (e && e->type() == QEvent::FocusIn)
    {
    // FocusIn detected - do stuff
    }
    return QMainWindow::eventFilter(e, o);
    }@

    Note that I don't even check the sender object, and I still never get the FocusIn event. How can I detect this event?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      StackedWidget is not a user interactive control so it doesn't have a focus event. What you can do is to install the event filter on each child widget of it and filter for that, eg.
      @
      auto widgetIndex = ...
      auto cldrn = stacWid->widget(widgetIndex)->findChildren<QWidget*>();
      for(auto obj : cldrn)
      obj->installEventFilter(whatever);
      @

      EDIT: You can get a focus on the stacked widget by setting a focus policy of the relevant page frames to StrongFocus, but it will only gain focus via keybord, not mouse, and not when children gain focus, so my previous advice holds.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        That is not a solution for me because I will dynamically add and remove widgets to and from StackedWidget. Installing event filter for each would require overriding the QStackedWidget and is pretty tedious, if at all possible. Is there no easier way?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well focus events don't usually bubble up so there's no direct way to capture them in a parent (for the most part).

          What you can do is, instead of filtering events, connect to the "QApplication::focusChanged":http://qt-project.org/doc/qt-5/qapplication.html#focusChanged signal and in the related slot check if the new widget with focus is a child of the relevant stacked widget page. You can do that by calling contains() on a findChildren() result (like in the example I gave earlier).

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            Thank you Chris, that's exactly what I was looking for!

            1 Reply Last reply
            0

            • Login

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