More a custom and practice question, but is it considered poor form, or highly inadvisable (choose your interpretation of badness) to mix the use of Qt library types and similar standard C++ types in the one program?
For example, is it highly advisable to stick to using ONLY QStrings for string use, or do people often mix QString and std::string types in the source file?
I have inherited some code that includes use of QString and std::string in the one file and am wondering whether to convert everything to one or the other.
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.
QStrings and C++ strings solve different problems - the main difference is that QStrings deal with locales/encodings explicitly, whereas std::strings do not.
so use QStrings for all data typed in by users, and all strings that are going to be displayed to your users, or you risk losing information in encoding conversions and/or making it harder to internationalize your app in the future.
For anything where I just need a char* equivalent, e.g. holding data read from a database where locales/encodings are not applicable, I use std::strings as they're a lot easier to use with non-Qt libraries and your data doesn't get put through any encoding conversions. You do, however, have to explicitly add them to the qt type system to be able to use them in signal/slot arguments, which is easy once you know how.
But in the end it's a judgement call - just for making your codebase more accessible to newcomers, it may be better to just use QString everywhere, or possibly just std::string everywhere if you're using QtCore in a non-gui app.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With