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

More customizable and practical question, but is it considered bad form or extremely impractical (choose your interpretation of badness) to mix the use of Qt library types and similar standard C ++ types in one program?

For example, is it really advisable to use ONLY QStrings for use in strings, or do people often mix the QString and std :: string types in the source file?

I have inherited some code that involves using QString and std :: string in one file, and I am wondering if I need to convert everything to one or the other.

+7
source share
5 answers

I think it really comes down to "it depends on what you do." Of course, it is easier to store things in only one type. However, there may be times when you need to pass std :: strings or pass QStrings, and it might be better not to do the conversion.

Also keep in mind that QStrings do not match std: strings. Therefore, keep this in mind when converting between them (or choosing only a specific type).

+7
source

Check out the blog post that compares STL with QTL and std::string with QString .

My 2 cents

It really depends on what you do. In general, when I code something where Qt not required (like a library), I always use STL . On the other hand, if I write code for a GUI application, I prefer to use QTL and QString over STL and std::string . If I want to integrate this code with a library written using STL , I provide overloaded functions that do the conversion from STL to QTL and QString to std::string .

QStrings are required. If you want to localize your application using tr() and QLinguist makes it dead easily.

+5
source

In general, I prefer to use QString on top of std::string and Qt containers (for example, QList , ...) on top of std containers in code that is closely related to the Qt map, in any case, it will not make sense. In all other components (e.g. helper libraries, etc.) I prefer to use the standard C ++ method for flexibility.

But I think this is rather a subjective decision, since types are usually pretty easy to convert from each other (and Qt containers also provide standard iterators with standard matching). Although in a highly competitive Qt container, Qt containers may work better, especially with the meta object and the Qt system.

+3
source

QStrings and C ++ lines solve different problems - the main difference is that QStrings explicitly processes locales / encodings, while std :: strings does not.

therefore, use QStrings for all data entered by users and all lines that will be displayed to your users, or you risk losing information in conversions and / or making it difficult to internationalize your application in the future.

For something where I just need the equivalent of char *, for example. storing data read from a database where locales / encodings are not applicable, I use std :: strings, since it is much easier to use with libraries other than Qt, and your data does not fall into any encoding transformations. However, you must explicitly add them to a qt type system in order to be able to use them in the signal / slot arguments, which is easy as soon as you know how to do this.

But in the end, this solution is just to make your codebase more accessible for beginners, it might be better to just use QString everywhere or maybe just std :: string everywhere if you use QtCore in a non-gui application.

+3
source

I think it's up to you. Consider, in any case, that the conversion between them requires a copy of the string, so if conversions are required to use both, this is not good from a performance point of view: read, for example, QString :: fromStdString and QString :: fromStdWString . If you use Qt, I think you can use not only QString, in which case most of the methods of Qt classes require you to provide QString. Thus, it can be bad for performance and inconvenient to write.

Keep in mind that std :: string changes slightly from implementation to implementation, and std :: string copy-on-write may show different performance.

+1
source

All Articles