How to get position and size of another program’s window in Qt?

Posted on Posted in Qt Tutorials, Tutorials

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.

#include “Windows.h”

  • 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”));
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:


2 thoughts on “How to get position and size of another program’s window in Qt?

  1. Just wanted to say that the information you provide is awesome. I’ve been developing my app for quite some time and stumbled upon some limitations (mostly running qt/android service that I’m currently researching on) either way this gives me great insight along with other posts you have written gives me a wider picture.

Leave a Reply

Your email address will not be published. Required fields are marked *