How to insert text into any Qt Window under Win32 programmatically from another app?
-
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, >i ))
{
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?
-
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);
}
@ -
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.