I'm writing a Win32 application that automates text input for several QLineEdit controls of a Qt program running in another process.
#include <qlineedit.h>
#include <qwindow.h>
#include <qguiapplication.h>
#pragma comment(lib, "qt5core.lib")
#pragma comment(lib, "qt5gui.lib")
#pragma comment(lib, "qt5widgets.lib")
void main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
HWND hMainWindow = FindWindow(L"Qt5QWindowIcon", L"Target QtApp");
if (hMainWindow != NULL)
{
QWindow* pWin = QWindow::fromWinId((WId)hMainWindow);
QLineEdit* pEdit = pWin->findChild<QLineEdit*>();
if (pEdit != NULL)
{
pEdit->setText("Hello");
}
else
{
QList<QLineEdit*> list = pWin->findChildren<QLineEdit*>();
if (!list.empty())
{
for (QLineEdit* pEdit : list)
{
pEdit->setText("Hello");
}
}
else MessageBox(NULL, L"List is empty.", L"Info", 0);
}
}
app.exec();
}
In the code above, I obtain a window handle (HWND) to the main window of the Qt app using the Win32 function FindWindow(). I get a valid handle with that call, then I convert it to a QWindow* pointer with QWindow::findWinId().
That call also returns a valid QWindow* pointer, which I then use to call:
QWindow::findChild<QLineEdit*>()QWindow::findChildren<QLineEdit*>()findChildren<QWidget*>()
However, all of these methods always return NULL or an empty list.
Why is this the case?
If this is not really possible with the code above, how to achieve what I'm trying to do in Win32, or maybe even in Qt?
Notes:
- The target Qt application is 32-bit, that's why I'm using Qt version 5.15.2, which is the last version that supports 32-bit Qt projects.
- I do not have direct access to the code of the target Qt application whose
QLineEditcontrols I'm trying to automate.