Upgrading to Qt 4.8.7 from Qt 4.8.5

Upgrading to Qt 4.8.7 on Windows

By Chongchen Saelee

I’m upgrading from Qt 4.8.5 to Qt 4.8.7. I’m also using QtCreator 2.8.0. This a step-by-step guide on how I successfully upgraded.

NOTE: Qt 4.8.7 requires newer minGW 64 compiler, so if you don’t install that correctly, it won’t work. Qt 4.8.7 SDK Installer does not come with minGW 64 compiler, so you have to download that separately. Continue reading “Upgrading to Qt 4.8.7 from Qt 4.8.5”

Warning! Qt Can Damage Your Computer

Don’t get me wrong, I love Qt, but they do make the disclaimer when you download and use their software. Just note that unless you have money to burn, if you keep pushing the limits of your hardware and operating system, odds are Qt will make it all crash and burn. For example, I’ve been able to compile and run “simple” programs endlessly without a problem. But once I start experimenting with QTransform or other heavy image processing things, my harddrive goes on the fritz or memory issues or graphics card chugs. It’s just not worth it if I have to replace these hardware. Therefore, just to put it out there for the hobbyists who might be curious: DO NOT RISK HAVING TO REPLACE YOUR HARDWARE BY EXPERIMENTING WITH QTRANSFORM OR QIMAGE IF, that’s IF YOU’RE GOING TO PUSH THE SYSTEM WITH 3D PROCESSING.

Mind you, that’s only if you’re messing with 3D stuff. You can mess with QPixmap and QImage for 2D stuff, but don’t mess with QTransform or QImage when it comes to 3D. It’s just too brutal on my hardware. I think it has to do with the nature of how Qt is designed: to do everything as fast as possible. And I’m assuming that include “unsafe” methods. I recall working with DirectX a bit and that’s why Microsoft wants you to use their “safe” DirectX functions vs “unsafe”, even though unsafe methods may yield faster results.

And I’m developing on a Windows 7, Pentium i5 3.4GHz Quad Core processor, new hard drive, new RAM, the works. So just be extra careful and aware. It’s not maliciously designed, it’s like that for a reason. Machine cannot regulate itself and always needs human intervention.

How To Embed Font in Qt Application Using QFontDatabase

You can embed a True Type font or Open Type font into your Qt application using QFontDatabase. You can link to an external font file or link to a font embedded as a resource.

First, make sure you include a reference to the QFontDatabase class:

#include <QFontDatabase>;

To link to an external font, do this:

QFontDatabase database;
int result = database.addApplicationFont("/path/to/font.tff");

Or for a more reliable way, embed the font as a resource. Create a Qt resource file and add a reference to the font file you want embedded. Then use the path to the embedded resource font:

QFontDatabase database;
int result = database.addApplicationFont(":/resource/path/to/font.tff");

If the returned integer is greater than -1, then the font has successfully loaded into the font database. It’s essentially storing the font in memory at the application’s runtime. You can check your operating system’s installed fonts and if the font hasn’t already been installed, that using QFontDatabase doesn’t install the font on your system. So this is a cool way of having specific fonts just for your Qt app without messing with the end-user’s fonts.

And to use the font, you would call it from the font database:

QFont f = database.font("myFont", "normal", 12);
myWidget.setFont(f);

Of course, be mindful of the font you choose for your app. As graphic designers, we are taught that for digital screens, you want to use sans-serif fonts (vs serif fonts for print). There needs to be good whitespace. Since users will be staring at the fonts for long periods of time, it needs to look nice and balanced overall. Then there’s also the issue of visual consistency and compatibility across operating systems. In this case, you don’t have to embed a custom font, but make sure you choose a web-safe font.

How to create X11 colors into Qt QColor Wrapper

You can mix any color you want nowadays with 32-bit colors. But sometimes, it’s just so much more convenient if the color palette already exists. And unfortunately, Qt only provides users with the most basic of colors. Therefore, it’s up to you to come up with a snazzy pre-mixed color palette yourself, but also to name those custom colors. But you don’t need to reinvent the wheel. The X Windows system and W3 web standard for colors and names already exists.

Unfortunately, if you’re using Qt on Windows, you won’t be able to access that color palette.

So this is what I did: I simply took the names and RGB values and created a wrapper class in Qt. That way I can easily access the colors via their names. Ain’t that a trip? It seems so simple to implement, but I haven’t found any tutorials out there on the Interwebs. It could save a lot of time for those that deal with a lot of colors.

Also, I whipped up a quick demo that displays the colors all nice and purdy.

X11 colors as QColor in Qt 4.8.5
X11 colors as QColor in Qt 4.8.5

Feel free to use this X11 color name wrapper class in your Qt project. Enjoy!

You can download the Qt 4.8.5 project files here:
X11_colors_qt

How to Implement Custom Optimized MouseDoubleClickEvent Function in Qt

If you were unaware that Qt’s default mouseDoubleClickEvent isn’t exactly optimized, as that is the nature of the beast, so when you override it, it will slow everything down. So the only thing you can do is write your own mouse double click event handler.

I won’t be using custom events and any fancy delegates and stuff. Don’t touch system pointers or direct memory unless you have to. This is a very simple implementation compared to the big picture.

To begin, you must understand that a mouse double click is essentially this sequence: mousePress, mouseRelease, mouseDoubleClickFlag, mouseRelease. Continue reading “How to Implement Custom Optimized MouseDoubleClickEvent Function in Qt”

Qt MouseDoubleClickEvent May Slow Down Performance

http://qt-project.org/doc/qt-4.8/qwidget.html#mouseDoubleClickEvent

I just discovered this. I’ve been implementing the convenience overriding of MouseDoubleClickEvent in all of my custom widgets. After optimizing all of my basic overrided mouse handling events: mousePressed, mouseMove, mouseReleased, it turns out it was that dang doubleclick that slowed it all down in the end. And all this time I thought I hadn’t written a tight optimized rendering loop.

Anyway, BE VERY CAREFUL with this. As it states in the reference documentation:

Note: The widget will also receive mouse press and mouse release events in addition to the double click event. It is up to the developer to ensure that the application interprets these events correctly.

That means that if you already have all your mousePress and mouseRelease custom overriding functions implemented, those will get called multiple times before it gets to the new override implementation of doubleclick, which will slow everything down exponentially. That doesn’t mean it’s a bug, that’s technically how a double-click is implemented at the internal level.

It looks like the only way to get optimal performance is to implement double-click yourself with a timer or variable.

Well, damn.

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: Continue reading “How to change screen resolution in Windows using Qt”

How to generate random number in Qt

If you thought Qt provided you with a robust count of math algorithms already, you’ll be in for a surprise. There is qrand(), but it doesn’t quite provide you with the user-friendliness of other languages. For example, sometimes you want a random number that is negative. Sure, you can do this all manually, but then you’d have to write your own function. Unfortunately, that’s exactly what you’ll have to do.

http://developer.nokia.com/Community/Wiki/Generating_random-value_integers_in_Qt

#include <QGlobal.h>
#include <QTime>

int QMyClass::randInt(int low, int high)
{
// Random number between low and high
return qrand() % ((high + 1) - low) + low;
}

// Create seed for the random
// That is needed only once on application startup
QTime time = QTime::currentTime();
qsrand((uint)time.msec());

// Get random value between 0-100
int randomValue = randInt(0,100);

It is recommended that you don’t use qrand() by default if you want to generate encryption-level random numbers.

So, with that code, now you can set a range of numbers which you can pull random numbers from. Make note that qsrand() only sets the seed and you should only do that once.