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. How to insert text into any Qt Window under Win32 programmatically from another app?
QtWS25 Last Chance

How to insert text into any Qt Window under Win32 programmatically from another app?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.9k 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.
  • T Offline
    T Offline
    Tramvon
    wrote on last edited by
    #1

    I have a code, which works with most windows apps - MSVC code editior window, Notepad, Wotdpad, MS Word/Excel. But it don't work with Qt wondows - as example, it don't work with QtCreator.

    @
    HWND hwndActiveWindow = GetForegroundWindow();
    if (hwndActiveWindow==0)
    {
    return;
    }

    DWORD activeWndProcessId = 0, activeWndThreadId = ::GetWindowThreadProcessId( hwndActiveWindow, &activeWndProcessId );

    GUITHREADINFO gti = { sizeof(gti) };
    if (!GetGUIThreadInfo( activeWndThreadId, &gti ))
    {
    return;
    }

    if (gti.hwndCaret==0)
    {
    return;
    }

    ::std::wstring::size_type i = 0, size = str.size();
    for(; i!=size; ++i)
    {
    ::PostMessage( gti.hwndCaret, WM_CHAR, (WPARAM)str[i], 1);
    }@

    Anybody can help me?

    1 Reply Last reply
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #2

      Hi, getting the hWndCaret is fairly new (introduced in Windows 2000) and Qt perhaps hasn't updated to it yet.

      So you need to go more medieval, i.e. use the older hWndFocus value instead, like this:
      @
      ....
      ....
      if (gti.hwndFocus==0)
      {
      return;
      }

      ::std::wstring::size_type i = 0, size = str.size();
      for(; i!=size; ++i)
      {
      ::PostMessage( gti.hwndFocus, WM_CHAR, (WPARAM)str[i], 1);
      }
      @

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tramvon
        wrote on last edited by
        #3

        New? Oh, you make me fun :) You know what year is it now? 2014. 2000 + 14 years. Six years later, in 2020, peoples will build towns on the moon :)

        Ok, thanks for hint, I will try to use hWndFocus.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tramvon
          wrote on last edited by
          #4

          SendInput with INPUT_KEYBOARD and KEYEVENTF_UNICODE solves the problem.
          Note: for Qt applications it need to send input event two times - first for key press and second with key release (with KEYEVENTF_KEYUP flag)

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chrisaverage
            wrote on last edited by
            #5

            It's not that Qt didn't update to the ( fairly new :P ) WinAPI stuff. It's that it chose loooong ago (somewhere around transition to Qt4) not to.

            In WinAPI every control is a window in its own right and gets all the windowing events (and some control specific ones). Because every control is a window it has its own HWND nad you can query it from outside via FindWindowEx and stuff like that. You can also post messages directly to these controls (like you did in your example).

            For many reasons (mostly performance and customization) Qt is not using native windows for controls. It basically renders everything to an offscreen pixmap and then blits it to the Win32 window. So from WinAPI point of view any Qt app is an empty window with just a custom background painting. You can't access any controls via WinAPI because there are none. It' similar with things like cursor and input events. Qt only interacts with WinAPI on the window (the frame) level and does all the internal eventing on its own types.

            That being said there are couple of things you can consider:

            Get a pointer to the main QWidget and use Qt methods to find children and post events (like "Squish":http://www.froglogic.com/squish/gui-testing/index.php does it for example).

            Another thing you can do is "force Qt to use native windows":http://qt-project.org/doc/qt-5/qwidget.html#native-widgets-vs-alien-widgets on specific controls or entire app. Do remember that this will have performance consequences and should be used sparingly if at all.

            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