Showing posts with label qt. Show all posts
Showing posts with label qt. Show all posts

Saturday, April 25, 2015

How-To: Create MySQL drivers in Qt for Windows


Hello! 


When installed, Qt lacks support for a series of SQL drivers such as that of MySQL, requiring the user to build them as he sees fit. This wouldn't be a major problem if there were easy-to-find manuals on the web or if Qt's own installation guide was clearer for starters - I remember it took something like 2 days for me to finally find the first working instructions when I first faced this problem (it was in Modh Amree's website which is now gone).

Regarding the not-so-much-new Qt 5, I found a clear instruction guide on Seppe Magiels' page (Create MySQL driver for Qt5 on Windows) which, as far as I can tell, is a correct (and clear) guide for doing this. The only significant difference between this guide and that of Amree's is that one step is lacking (probably due to changes in the Qt version) which is to repeat items 3 and 4 of the Step 4, but with "CONFIG+=release" appended to the call to qmake.


May the Lord be with you, always.

Friday, June 20, 2014

Tips and Tricks: Configuration depending on the compilation type when using qmake

Hello!

One of the things almost all projects that use Qt with qmake will end up having is alterations to the .pro file. Depending on the necessities of the project, many of these alterations will have to be different depending on wheter the user is doing a debug or a release compilation. Lets have a look into this.

Default: Scopes

If one have a look in the Qt Assistant files, this "configuration given a certain kind of compilation" should be done by scopes. The "scope mechanism" consists of declaring which king of compilation you're talking about (either "release" or "debug") together with the configuration data inside a group created with the {} symbols. Example:

release {
    LIBS += ...
    }

debug {
    LIBS += ...
    }

A better way

Theorically the scopes method works fine and it should do the trick. The problem, though, is that, by default, a project created in Qt Creator with qmake comes with some configurations to qmake that usually includes both release and debug situations. In other words, normally the scopes mechanism will fail. 

This problem have two known solutions: either you change the default configurations or you change the method used for specific configuration in the .pro file. The last option (that, at the end, should be used either way) consists of using the CONFIG directive in the .pro file this way:

CONFIG(debug, debug|release) {
    LIBS += ...
    }

CONFIG(release, debug|release) {
    LIBS += ...
    }

In the first code, the configuration is related to the debug compilation; in the second, is related to a release compilation. Put your code inside and everything will work without having to worry about the default configurations.

Conclusion

In this small article we saw a better way of writting configuration parameters in the .pro file of one's project using the CONFIG qmake directive instead of the default "scopes method"; an alternative that should be preferred to avoid having to unnecessarily change qmake's default configurations when a new project is created.

God bless you! Have a nice day!

References


Friday, May 30, 2014

Tips and Tricks: Declaring and defining a QObject-derived class

Hello!

The tip of the day is the following:

If you're creating a class that inherits QObject (and therefore uses the Q_OBJECT macro), defining it on the .cpp file will not work. All classes that has such macro needs to be defined in a header file.
So being the case, here comes a tip: if your "main" class requires some secondary classes (and at least one of them is a QObject-derived one), then put all this classes inside a separated header file and include that file in your "main" class' header file. This way you not only skirted the problem, but also added some better organization to your codes.

God bless you! Have a nice day!

References

Saturday, May 24, 2014

How-To: Convert struct in and from a QByteArray

V1.2
Hello!

Recently I wrote a post about a better way of performing type casting in C++ when needing the use of the problematic reinterpret_cast. Now I woud like to write some comments about another "casting", namely between a struct and a QByteArray.

Conversion using QDataStream

A conversion from a struct to a QByteArray can be usefull when somebody, for example, handles some data inside a struct and want to send it through a interface that works with QByteArray such as QIODevice. At the other side of the system, the received QByteArray may be needed to be converted back to a defined struct for proper handling.

In accordance to some references, the proper way of doing this is by constructing serialization and de-serialization methods specifically for doing such a job using QDataStream.

Here is a example for a serialization method:

QByteArray serialize()
{
    QByteArray byteArray;

    QDataStream stream(&byteArray, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_4_5);

    stream << status_change_id
           << result_code
           << reserved[0]
           << reserved[1]
           << reserved[2];

    return byteArray;
}

And here how to deserialize some data:

void deserialize(const QByteArray& byteArray)
{
    QDataStream stream(byteArray);
    stream.setVersion(QDataStream::Qt_4_5);

    stream >> status_change_id
           >> result_code
           >> reserved[0]
           >> reserved[1]
           >> reserved[2];
}

Other options

Another option (now exclusivaly about converting from a QByteArray to a struct) is doing a cast of the QByteArray to a pointer of void and then static-casting to the desired struct, either in a const or non-const way:

void convertToStruct(const QByteArray& byteArray)
{
    //constData() used because const is desired; otherwise, prefer data() don't forgetting deep copy issues
    const void* poTemp = (const void*)byteArray.constData();
    const MyStruct* poStruct = static_cast< const MyStruct* >(poTemp);
}

Conclusion

In this article we saw how to do proper casting between a struct and a QByteArray, something that may come in handy many times when working with more advanced applications. About the usage of QDataStream for doing this work, I'ld suggest a reading on this forum thread where I had a discussion about the usage of QDataStream for such context. Another interesting reading would be this other thread where I discussed about other methods of doing this work and some other, more generalized suggestions were discussed.


God bless you!
Have a nice day!

References