How to change screen resolution in Windows using Qt

If you, too, are scouring the Interwebs for the answer, here it is:

First, you’re going to need to download the Windows SDK to get the proper c++ header files to reference. In this particular case, you’re going to want to reference “windows.h” like so:

#include "windows.h"

And then access the ChangeDisplaySettings function from the Windows API like so:

dm->dmPelsWidth=(unsigned long)800;
dm->dmPelsHeight=(unsigned long)600;

And last but not least, you need to set your form/widget into fullscreenMode like so:


And what you get is the intended, most importantly OPTIMIZED minimal screen for drawing your graphics to. The idea is you don’t want to draw to fill up the end-user’s high resolution screen unless you have to, especially when it’s a simple 2D game.

And if you want to return to you default display resolution:

ChangeDisplaySettings(NULL, 0); //Windows API call

//Reset Qt window state
this->move(0,0); //in case it was moved out of reach

But do note, this isn’t a complex implementation. If your local display driver, say Nvidia or ATI graphics driver is broken, then it won’t auto-magically fix the aspect ratio or whatever. That’s the case with my Windows 7 development machine and I have to set it to 640×480 manually via Nvidia Control Panel. Otherwise, this Windows API code only changes the resolution.

Oh, and before I forget and you noobs scream faulty coding, don’t forget to code somekind of escape mechanism to return back to normal. For example, set up the keyPressed event on the form/widget to capture the ESCAPE key so that you can exit/close the application, thus returning your resolution back to what it originally was. I don’t know if this is a given, but it seems to work like that on Windows Vista and Windows 7.

Otherwise, I make no promises of it being safe code. But if it works for you, have fun.