Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

std

qt

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.

like image 629
Pete855217 Avatar asked Nov 06 '11 12:11

Pete855217


4 Answers

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).

like image 146
Trenton Schulz Avatar answered Nov 20 '22 06:11

Trenton Schulz


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.

like image 27
pnezis Avatar answered Nov 20 '22 04:11

pnezis


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.

like image 4
je4d Avatar answered Nov 20 '22 06:11

je4d


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.

like image 3
Christian Rau Avatar answered Nov 20 '22 04:11

Christian Rau