You can use Windows API functions to read the position (left and top, or x and y) of a window that does not belong to your program in Qt. GetWindowRect function allows you to read these values and also right and bottom which can then be used to extract width and height of that window. Note that you need to know the title of your target window or else it is not possible to achieve that. Follow th simple steps below to be able to do this.
- Add the following to your .PRO file (qmake project)
LIBS += -lUser32
- Now add the following t your header file.
- Using the following piece of code you can read X, Y, Width and Height of any Window that you are aware of its title.
HWND h = FindWindow(NULL, TEXT("Some Window Title")); LPRECT rct; GetWindowRect(h, rct); qDebug() << "X = " << rct->left; qDebug() << "Y = " << rct->top; qDebug() << "Width = " << rct->right - rct->left; qDebug() << "Height = " << rct->bottom - rct->top;
Notice that you have to use a function named FindWindow to get access to the Window using its title.
And then you pass a variable with type LPRECT to GetWindowRect and it fills it with the results.
Download example project from here: