Qt Development – Prefer Qt Functions, Data Types, and Classes

cdata typesideprogramming practicesqt

I'm just starting out with Qt and I really want to try and keep my application as separated from Qt as possible in case I decide to use a different toolset later, but at the same time don't want to make any decisions that will really cripple my application right from the beginning.

When writing a Qt Application is it considered good practice to always prefer Qt implementations when they are available? Or in some situations is it best to stick with standard C++ even when Qt has an alternative?

Consider the following…

Function implementations:

Should I always prefer to use a Qt function implementation when it is availabe? Take the math functions for example…

  • pow vs qPow
  • log vs qLn
  • etc…

Data types:

Should I always prefer Qt data types over the defaults?

  • int32_t vs qint32
  • double vs qreal
  • etc…

Objects:

Should I always prefer Qt objects over their STL equivalents?

  • std::string vs QString
  • std::vector vs QVector

In each of these situations what are the advantages/disadvantages. What will I gain or lose?

The more Qt based functions/objects I use the more difficult it would be to switch to something other than Qt later.

Best Answer

If there is an exact equivalent in the C++ standard (like for exact-width types, or math functions), you loose portability to any project not using QT, without gaining anything. And people might wonder what that function / type / whatever does or does differently if they are not intimately knowledgeable about QT.

There are two reasons for using QT object types like QString and QVector:

  1. Independence from the C++ library you use. This is only an advantage if you expect it to have breaking changes more often than QT, and cannot / will not provide an update.
  2. Adherence to the interface of functions you use.

Those two points apply in reverse to using the standard types instead.

Related Topic