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

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:

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.

#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();

// 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.

Overriding QWidget Events

For the most part, Qt comes with a bunch of convenience widgets built on top of their own QWidget. They may provide very specific features and might suit your needs perfectly. But when you want to implement something that is really unique, then you’ll have to be able to engage the internal processes that make your user-friendly interactions so intuitive. One way is to override QWidget’s default events. Continue reading “Overriding QWidget Events”

Hello World with QPainter

This is the most basic of basic tutorials when it comes to computer programming. It is virtually done to death. The basic idea is for you as the programmer to be able to send a message to the computer and have it return the message to you as output. Originally, this would be done via command-line or command-prompt, a scary black void of an interface. So in this case, I’ll show you how to actually engage the user by hooking into the default GUI mainWindow. Continue reading “Hello World with QPainter”