I saw an example that uses the following to create a point that will be used to centre a window in Qt:
x = (screenWidth – WIDTH) / 2;
y = (screenHeight – HEIGHT) / 2;
Provided that screenWidth and screenHeight are found using the width() and height() functions respectively of QDesktopWidget.
How does the preceding code centre the window? Yes, I know it centres the window, but couldn’t understand it from a calculation point of view.
Thanks.
First calculate the total amount of ‘extra’ horizontal space around your window:
now, spread that space at left and right:
the space at both sides should be the same:
that’s your
x. The same goes for theycoordinate.