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

Friday, May 16, 2014

Tips and Tricks: Proper way of "reinterpret casting"

Hello!


This tutorial doesn't talk about a Qt programming tip, but of a matter of general concern in C++ (and some in C) programming: type casting.


Concept and general knowledge

To remember the concept, type casting means to interpret a given data as a different type from the one it's currently being hold, like making a unsigned char variable be seen as a signed integer.

There are various types of casting in C++ (which also includes C casting) and the best explanation I ever found about them may be found in this stackoverflow thread.

Reinterpret casting correctly

The most accepted answer in the stackoverflow thread linked above notes that there is a particular casting method, namely reinterpret_cast, which is particularly bad to use. MSDN article about casting notes:

We recommend that you not use reinterpret_cast because neither a compile-time check nor a run-time check is performed. In the worst case, a reinterpret_cast makes it possible for programming errors to go undetected at development time and cause subtle or catastrophic errors in your program’s behaviour.
The remark is followed by a generally accepted advice:

Therefore, we recommend that you use reinterpret_cast only in those rare cases when you must cast between unrelated types and you know that the cast will succeed.

And in another link:

Unless the desired conversion is inherently low-level, you should use one of the other cast operators.
The question is: should we never, then, use reinterpret_cast? And what about when we actually need it?


The best solution to this problem I found came from a thread in a LinkedIn group, namely C++ Developers Group. When discussing about failure when doing type casting, user Evgeny P., a software development consultant at Visual Technology Services Ltd. at the time, suggested that instead of using normal reinterpret_cast


T1 *p1= ... ;
T2 *p2=reinterpret_cast<T2*>(p1);
, one should prefer do a static_cast after a cast to void*:

T1 *p1= ... ;
void *pV=p1;
T2 *p2=static_cast<T2*>(pV);

or, in case the user wants a more "clear intent code", one should use something like:

template<typename To> inline
To *unrelated_pointer_cast(void *from)
{
    return static_cast<To*>(from);
}

Req *msg = unrelated_pointer_cast<Req>(const_cast<U8*>(msgPtr));

Conclusion

Good programming means not simply "code that works", but "code that works well", and one way of archiving this is by using ways of doing tasks in a better way, if so is available, either by means of using safer code or another way. With his tip, one may improve his code by avoiding the use of reinterpret_cast without having to change anything else in his work.


God bless you! Have a nice day!

Saturday, May 3, 2014

Introducing Templates

Hello!


In the next days I'll start posting a new kind of post called templates containing "template codes". A template code is a code which serves as a base for development and over which the user may freely edit it to  the way it best suit his needs. It's a practical way of learning how to use a class or a couple of methods without having to read the some times boring help files. It's also different from a example because it's not necessarely a compilable code (it lacks the details that makes a code compilable, such as a .pro file in Qt) and lacks the normal adjustments required for a example to be a acceptable one (like style and layout adjustments).


God bless you! Have a nice day!

Static: Tips, tricks and tutorials based on subject

This page is dedicated to lots of tips, tricks and tutorial material grouped by subject, such as "Qt Quick" or "Qt Embedded".

Note: for a list with general material also grouped by subject, visit "Static: Content based on subject".


General and conceptual

Debug

Performance

Translation

Others



General coding


Memory

Others



Qt Embedded



Qt Quick



Qt Creator




Static: Content based on subject

This page is dedicated to lots of general material grouped by subject, such as "Qt Quick" or "Qt Embedded".

Note: for a list with specific material also groupped by subject, visit "Static: Tips, tricks and tutorials based on subject".


Qt Creator


Qt Embedded


Benchmarks

How-to benchmark

Others

  • qtperf - Application to test Qt graphics performance 










Static: Websites categorized by type

This post contains a list of Qt-related webpages cathegorized by type that are found around the web.

Feel free to tell me about one you found that is not on the list in the comments section below!

Generic 

Pages that are related to Qt, but not with a distinctive specific style such as a forum, wiki or blog)

Blogs

Forums

By language:

Wikis

Applications

Websites dedicated to Qt apps

Libraries

Websites devoted to Qt libs

Others