2

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.

  • In my opinion it's not always a good practice, it just depends on your need, the design you have. But I prefer to stay consistent and use Qt classes when the code is already full of Qt that cannot be standard C++ anyway. – ymoreau Jan 25 '18 at 14:54
  • @ymoreau: I am starting a project fresh so I am free to choose. I am deciding to go with standard C++ until I encounter a situation where Qt seems to give me trouble and needs to use its own objects. :) – tjwrona1992 Jan 25 '18 at 14:55
  • I meant if you have some needs that STL does not cover and you know you will use different Qt modules anyway, these modules will need some Qt types (QString, Qt containers, signal/slots stuff) and you might prefer to use Qt in the rest of your code than doing a lot of conversions, again depending on the design you have ;-) – ymoreau Jan 25 '18 at 14:57
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.