Qt - mixing Qt and std:: C++ types

I think it really comes down to “it depends on what you are doing.” Certainly, it is easier to keep things as only one type. Yet, there may be times when you need to pass std::strings or pass QStrings and it might be better performance-wise to not do the conversion.

Also keep in mind that QStrings are not the same as std:strings. So keep that in mind when converting between them (or choosing to only use a certain type).


Check this blog post that compares STL to QTL and std::string to QString.

My 2 cents

It really depends on what you are doing. In general when I am coding something where Qt is not necessary (e.g. a library) I always use the STL. On the other hand if I am writing code for a GUI application, I prefer to use QTL and and QString over STL and std::string. If I want to integrate this code with a library that is written using STL, I provide overloaded functions that make the conversion from STL to QTL and QString to std::string.

QStrings are a must If you want to localize your application since using tr() and QLinguist makes it dead easy.


In general I prefer to use QString over std::string and Qt containers (like QList, ...) over std containers in code that is tightly coupled to the Qt framework, anyway and wouldn't make sense without it. In all other components (like auxiliary libraries and the like) I prefer to use the standard C++ way of things, for the sake of flexibility.

But I think this is more of a subjective decision, as the types are usually quite easy to convert from one another (and Qt containers also provide standard conformant iterators). Although in highly Qt centric code the Qt containers might work better, especially together with Qt's meta object and type system.

Tags:

C++

Qt

Std