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. Window steals focus
Forum Updated to NodeBB v4.3 + New Features

Window steals focus

Scheduled Pinned Locked Moved Unsolved General and Desktop
focus
9 Posts 4 Posters 7.1k Views 2 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.
  • B Offline
    B Offline
    Ben van Gompel
    wrote on last edited by Ben van Gompel
    #1

    Hi!

    We have an application consisting of two windows. During runtime we add and remove widgets from those windows, initiated by actions on one of the windows (say Window1). What happens is that focus changes from Window1 to Window2 as a consequence of the widgets changing on Window2, and as far as I can see there is no code that explicitly does that.

    So, my question is: what can make a window obtain focus? Are there are specific Qt calls that may lead to a window gaining focus?

    I understand this may be a bit vague, especially since it is lacking code snippets, but initially I want to get a general understanding of focus policy in Qt.

    Thanks, Ben

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      Generally when you show a new widget, it gets the focus. one thing you can try is to call setFocus on your Window1 widget.

      You also have more information on focus handling here

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      1
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        hard to tell what happens without seeing the code.
        The issue mentioned by SGaist should only kick in when you call show() on your widgets before setting the parent. So they result in a separate window for a short period of time. Activating windows get the input focus.
        You can prevent this by calling setAttribute(Qt::WA_ShowWithoutActivating) on the widget before showing.

        But to make sure set a breakpoint in the focus-in/out event handler and check in the method call stack what caused the focus change.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        B Z 2 Replies Last reply
        2
        • raven-worxR raven-worx

          hard to tell what happens without seeing the code.
          The issue mentioned by SGaist should only kick in when you call show() on your widgets before setting the parent. So they result in a separate window for a short period of time. Activating windows get the input focus.
          You can prevent this by calling setAttribute(Qt::WA_ShowWithoutActivating) on the widget before showing.

          But to make sure set a breakpoint in the focus-in/out event handler and check in the method call stack what caused the focus change.

          B Offline
          B Offline
          Ben van Gompel
          wrote on last edited by
          #4

          @raven-worx Thanks!
          Turns out the focus change is indeed being caused by a ActivatedWindow event. So, now I have to figure out where that came from :-)

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Generally when you show a new widget, it gets the focus. one thing you can try is to call setFocus on your Window1 widget.

            You also have more information on focus handling here

            Hope it helps

            B Offline
            B Offline
            Ben van Gompel
            wrote on last edited by
            #5

            @SGaist Thanks!
            As far as I know we don't directly call show on the new widget, but there is this 3rd party code, which may just do that.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Ben van Gompel
              wrote on last edited by
              #6

              So, it took me some time to figure out what happened. Holiday break didn't help either :-) Happy new year all!

              It turns out, the problem is caused by our use of QTabWidget, which internally uses QStackedWidget. We can call setAttribute(Qt::WA_ShowWithoutActivating) on the QTabWidget but that does not seem to affect the internal QStackedWidget.

              Besides writing our own QTabWidget version I don't see a way around this.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                One thing you can try is to use findChild on your QTabWidget to get the internal QStackedWidget and apply the attribute on it.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                B 1 Reply Last reply
                0
                • SGaistS SGaist

                  One thing you can try is to use findChild on your QTabWidget to get the internal QStackedWidget and apply the attribute on it.

                  B Offline
                  B Offline
                  Ben van Gompel
                  wrote on last edited by
                  #8

                  @SGaist Thanks

                  Further debugging shows my problem is not with 'show' but with 'setParent'. Somehow, that results in the window being activated. Seems to happen in external (for me) code.

                  Qt5Guid.dll!QWindowSystemInterfacePrivate::ActivatedWindowEvent::ActivatedWindowEvent(QWindow * activatedWindow, Qt::FocusReason r) Line 144    C++
                  Qt5Guid.dll!QWindowSystemInterface::handleWindowActivated(QWindow * tlw, Qt::FocusReason r) Line 110    C++
                  qwindowsd.dll!QWindowsContext::handleFocusEvent(QtWindows::WindowsEventType et, QWindowsWindow * platformWindow) Line 1178    C++
                  qwindowsd.dll!QWindowsContext::windowsProc(HWND__ * hwnd, unsigned int message, QtWindows::WindowsEventType et, unsigned __int64 wParam, __int64 lParam, __int64 * result) Line 1037    C++
                  qwindowsd.dll!qWindowsWndProc(HWND__ * hwnd, unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 1236  C++
                  [External Code]    
                  qwindowsd.dll!qWindowsWndProc(HWND__ * hwnd, unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 1245  C++
                  [External Code]    
                  qwindowsd.dll!QWindowsWindow::setParent_sys(const QPlatformWindow * parent) Line 1252    C++
                  qwindowsd.dll!QWindowsWindow::setParent(const QPlatformWindow * newParent) Line 1226    C++
                  Qt5Guid.dll!QWindow::setParent(QWindow * parent) Line 596    C++
                  Qt5Widgetsd.dll!QWidgetPrivate::setParent_sys(QWidget * newparent, QFlags<enum Qt::WindowType> f) Line 10521    C++
                  Qt5Widgetsd.dll!QWidget::setParent(QWidget * parent, QFlags<enum Qt::WindowType> f) Line 10376 C++
                  Qt5Widgetsd.dll!QWidget::setParent(QWidget * parent) Line 10311    C++
                  Qt5Widgetsd.dll!QLayout::addChildWidget(QWidget * w) Line 929    C++
                  Qt5Widgetsd.dll!QStackedLayout::insertWidget(int index, QWidget * widget) Line 223    C++
                  Qt5Widgetsd.dll!QStackedWidget::insertWidget(int index, QWidget * widget) Line 178    C++
                  Qt5Widgetsd.dll!QTabWidget::insertTab(int index, QWidget * w, const QIcon & icon, const QString & label) Line 457    C++
                  Qt5Widgetsd.dll!QTabWidget::addTab(QWidget * child, const QIcon & icon, const QString & label) Line 400    C++
                  
                  1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    hard to tell what happens without seeing the code.
                    The issue mentioned by SGaist should only kick in when you call show() on your widgets before setting the parent. So they result in a separate window for a short period of time. Activating windows get the input focus.
                    You can prevent this by calling setAttribute(Qt::WA_ShowWithoutActivating) on the widget before showing.

                    But to make sure set a breakpoint in the focus-in/out event handler and check in the method call stack what caused the focus change.

                    Z Offline
                    Z Offline
                    ZoltanSz
                    wrote on last edited by
                    #9

                    @raven-worx said in Window steals focus:

                    when you call show() on your widgets before setting the parent. So they result in a separate window for a short period of time.

                    This! Have been looking for my focus problem for a while now, and this was what I missed. Thanks!

                    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