How to change screen resolution in Windows using Qt

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

http://www.qtforum.org/article/30063/qt-opengl-resolution.html?s=404deefd560e1ab54cce082517049711d0fdb836#post100696

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:

DEVMODE *dm=new DEVMODE();
EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,dm);
dm->dmPelsWidth=(unsigned long)800;
dm->dmPelsHeight=(unsigned long)600;
ChangeDisplaySettings(dm,CDS_FULLSCREEN);

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

this.setFullScreen();

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.

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.

This entry was posted in Qt. Bookmark the permalink.

Leave a Reply

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