Thursday, August 28, 2014

New programming jargons

Hello there!

This is my first post that is of the "off-topic" class: although still talking about programming, it's not directed to Qt or C++ programming specifically.

Some time ago I was doing some research on the web and ended up finding this most interesting post in another blog called "New Programming Jargon".

It talks about a thread of Stack Overflow that was directed for people to talk about new programming jargon they have created and that were nice enough to be shared among the community - and actually most of them are pretty good!

So, have a look at it and lets code no "Yoda Conditions" anymore!


God bless you!

Have a nice day!

Sunday, July 20, 2014

Logging classes and libraries for Qt

V1.3
Hello!

Recently I had to do a research on logging systems for Qt-based applications and I found out that, while there is no system in Qt itself for such work (no "QLogger" class), there are a variety of works of the genre freely available on the web. Here is a list with the ones I found and some commentaries.


Logging mechanisms

Logger

Link: Gitorious repository
References: Ref 1
  • Thread safe
  • No "fixed size" log file system
  • Qt5 supported

QxtLogger

Link:
Help files: QxtLogger Class Reference
References: Ref 1
  • Thread safe
  • No "fixed size" log file system

QsLog

Link: Bitbucket link
Help files: QxtLogger Class Reference
References: Ref 1 Ref 2 Ref 3
  • Log level configuration for the entire log¹

QLogger

Link: Github repository
References: Ref 1
  • Log level configuration for each file one wants to print¹
  • LGPL license

QLogger

Link: Google repository
  • GPL license
  • Qt4-based

QLogger (QtPlayground)

Link: QtPlayground repository
  • Characteristics not yet verified.


Qtilities Log System

References: Ref 1
  • Characteristics not yet verified.

QtPlayground/QLogger

Link: Gitorious repository
  • Characteristics not yet verified.

Log4Qt

Link: Sourceforge repository
  • Quite a big library¹
  • APACHE license

Qt Logger

Link: Gitorious repository
  • Characteristics not yet verified.

Logger

Link: Download link and reference
  • Characteristics not yet verified.

mLogger

Now this is my own logging class! Unfortunately it's not yet public available, but the following list shows its features or planned features:
  • Usable for log into a file and for debug in Qt Creator's console at the same time
  • Fixed-size log file system
  • Thread safe
  • Debug with file + method + line information


Others

I also found some interesting links telling a little about logging mechanisms (not necessarely for C++). I guess they may be usefull if, despite the considerable number of logging classes or libraries for Qt shown above, one still wants to develop a logging mechanism of his own:


God bless you!
Have a nice day!


A nice FAQ about C++

Hello!

I recently found an interesting FAQ page about C++ with interesting information about some commons doubts about C++. Here is the link.


God bless you!
Have a nice day!

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

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



Saturday, April 12, 2014

Welcoming, first message

Hello, guys!


My name is Martin G. B. Bittencourt, a brazillian Control and Automation Engineer who found in Qt a very nice tool for software development.

Now-a-days I'm not a "Qt guru", that is, there is lots of things for me to learn about Qt and how to best improve my coding. But a problem I found out when doing research on the web was that Qt-related materials were too much widespread accross the internet, sometimes needing lots of Google search in order to find the right tip for doing something. So I decided to write the Smart Qt blog, a blog that will work as database for people to find all this amazing content about Qt that is around the Internet in a organized way - what also means that I'll post few things from my own work here.

I just created this blog, so sorry for not having any content yet! I hope till tomorrow I'll have already organized this instrument, and by the end of the next week it will already be working properly. And also, I'm not a native English speaker although I try my best! So sorry for any wrong spelling you may find on this blog!

Thanks for the patience! :)


God bless you!
Have a nice day!