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. Focus problems when embedding a native window
Forum Updated to NodeBB v4.3 + New Features

Focus problems when embedding a native window

Scheduled Pinned Locked Moved General and Desktop
win32desktop
5 Posts 3 Posters 4.8k Views 3 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.
  • Z Offline
    Z Offline
    Zylann
    wrote on 20 Jul 2015, 16:21 last edited by
    #1

    Hello,
    In our application we embed content that is not necessarily Qt-based, such as WPF or custom windows (we don't control how these windows are created, we just receive a native handle).
    To do this, we use this kind of code:

    // In our widget embedding the non-Qt content
    m_window = QWindow::fromWinId((WId)hwnd);
    m_window->setFlags(Qt::FramelessWindowHint);
    m_window->show();
    m_windowContainer = QWidget::createWindowContainer(m_window, this, Qt::FramelessWindowHint);
    m_windowContainer->setParent(this);
    m_windowContainer->show();
    

    It seems to work fine, however we experience focus problems, such as mouse wheel not working.
    When the application is unfocused, then focused again by clicking first on our embedded window, it's like we clicked on the QMainWindow instead...
    By hooking the WindowProc of one of our HWND, we noticed it never receives WM_FOCUS back.

    We tried to override focusInEvent() in our widget, however it doesn't get any event, since the embedded content is placed in front of it...

    I know there are limitations when doing this, however that's not what I expected by reading the doc. I found several topics more or less related to this issue, but couldn't find an answer yet. Is there something to do we missed?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 21 Jul 2015, 00:14 last edited by
      #2

      Hi,

      @Zylann said:

      It seems to work fine, however we experience focus problems, such as mouse wheel not working.
      When the application is unfocused, then focused again by clicking first on our embedded window, it's like we clicked on the QMainWindow instead...
      By hooking the WindowProc of one of our HWND, we noticed it nev

      Unfortunately, there are multiple issues with embedding external windows into QWidgets, but they're not being actively addressed. See https://bugreports.qt.io/browse/QTBUG-40320

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      Z 1 Reply Last reply 21 Jul 2015, 08:27
      0
      • J JKSH
        21 Jul 2015, 00:14

        Hi,

        @Zylann said:

        It seems to work fine, however we experience focus problems, such as mouse wheel not working.
        When the application is unfocused, then focused again by clicking first on our embedded window, it's like we clicked on the QMainWindow instead...
        By hooking the WindowProc of one of our HWND, we noticed it nev

        Unfortunately, there are multiple issues with embedding external windows into QWidgets, but they're not being actively addressed. See https://bugreports.qt.io/browse/QTBUG-40320

        Z Offline
        Z Offline
        Zylann
        wrote on 21 Jul 2015, 08:27 last edited by
        #3

        @JKSH there might be a slight difference in my case actually:
        I'm not trying to embed a window from another application, but a HWND created by my own.
        Does it makes a difference, or the problem is the same?

        J 1 Reply Last reply 21 Jul 2015, 08:43
        0
        • Z Zylann
          21 Jul 2015, 08:27

          @JKSH there might be a slight difference in my case actually:
          I'm not trying to embed a window from another application, but a HWND created by my own.
          Does it makes a difference, or the problem is the same?

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 21 Jul 2015, 08:43 last edited by
          #4

          @Zylann said:

          @JKSH there might be a slight difference in my case actually:
          I'm not trying to embed a window from another application, but a HWND created by my own.
          Does it makes a difference, or the problem is the same?

          Same problems. By "external", I meant "not made by Qt Widgets".

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • F Offline
            F Offline
            faz.cle
            wrote on 21 Feb 2019, 09:54 last edited by faz.cle
            #5

            This thread is 4 years old, but I want to give a solution anyway.
            I spent the past 2 months trying to figure out how to do this, and all the threads I found are talking about issues but nobody gave me a solution.

            Actually, the QWidget you create has its own focus but is not "parent" of the HWND you are embedding on it, so you have to make the keyboard focus by yourself.

            The best way to do that is by using the SetFocus function of the winuser library (WinUser.h):
            https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setfocus

            Like, in example:

            m_qwindow	= QWindow::fromWinId(m_handle);
            m_widget	= QWidget::createWindowContainer(m_qwindow, this);
            
            // tell the os to focus the viewport window
            SetFocus(m_handle);
            

            with m_handle being the HWND (or WId) used to create the QWindow.

            Unfortunately, this solution works only on windows (but maybe there are equivalents on another os).
            It also doesn't work when clicking on the embedded window, because it isn't owned by Qt.
            maybe you can try to mess with QFocusFrame (I haven't tried this at all, but it should be a way to use it as a fix by putting it over the embedded window)
            or, if the window you are embedding is one of your creations, put the SetFocus() function in its code, as a callback when clicked.

            I hope that even with 4 years late, this solution is still helping the desperate developers like who I was the past two months.

            1 Reply Last reply
            2

            • Login

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