Tips and tricks for c++ pointers and operators

If you really want to get the best performance from your c++ coding adventures, you’re gonna have to really get deep into the low-level experience. That means learning how to use pointers and directly accessing memory and data, which is always risky, but with care and proper practices, it shouldn’t even make you flinch. So here are some tips on how to master even c++ pointers and operators.

Usually when you’re accessing a pointer to a member of a class or object, you can do so in c++ like this:

//accessing member of object 10 in the someObject array/list
someObject[10].member

The [] operator is no different than this: Continue reading “Tips and tricks for c++ pointers and operators”

How to use Windows API all-purpose PVOID* structure

If you’re into modern Windows API c++ programming, it seems things have gotten just slightly easier. Naming conventions for classes, functions, structs, enums, etc. just aren’t as confusing anymore. I’m sure technical experts know how to move virtual memory around, which is essentially what programming and problem solving is, but if you’re just sitting down for some casual tinkering, it’s going to be a nightmare. You’re relying on the developers before you to name things intuitively.

So now Windows API documentation has been trying to promote using their generic PVOID struct more. It’s safer, maybe. It’s nothing special. It’s a generic struct to get a pointer to some data, any data. But you have to cast the data type yourself.

Here’s an example of how you can do something: Continue reading “How to use Windows API all-purpose PVOID* structure”

Knowing when to use [ ] vs .at( ) in Qt to read vs write data to objects

Knowing when to use [ ] vs .at( ) in Qt to read vs write data to objects

In Qt c++, you can directly access members of an object, whether it’s a class or struct, by using the square brackets [ and ] after the name of the object. For example:

classObject[0].classMember

Using square brackets [ and ] on an object allows you to directly READ AND WRITE to the member. However, knowing the difference between WHEN to use it could help increase the performance of the data handling cycles. In order to directly access that member data, and to allow read AND write access, Qt has to create a new pointer to the object itself in memory to do so. And creating pointers costs a lot of memory. And if you’re dealing with hundreds of thousands of objects and their hundreds of members, then things can get sluggish. Continue reading “Knowing when to use [ ] vs .at( ) in Qt to read vs write data to objects”

How To Read EXIF Orientation From JPEG in Qt 4.8.5

For many Qt lovers out there who might want to use third-party EXIF parsing library with their Qt apps, it may prove difficult unless you know what you’re doing. If you’re a hobbyist like me, man, it’s just tough. But there are free EXIF parsers out there, and it might take a little elbow grease to make it work with Qt. In my case, I put together a very simple and basic image viewer that I thought users might appreciate if it auto-rotated any images that contained EXIF metadata such as orientation. In smartphones and DSLRs, there is usually a sensor that records which orientation the picture was taken in. So when you view the photo without orientation applied, it might be a minor nuisance having to manually rotate it. That’s where you come in as software developer and take care of it for the end-user. Continue reading “How To Read EXIF Orientation From JPEG in Qt 4.8.5”

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.

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”