Difference between QString and std::string

Whats the difference between QString and std:: string and why can't I say:

 QString name; std::string name1; name = name1;

although both are strings

Thanks in advance and sorry for my bad english

2

4 Answers

How would the compiler deduce that "both are strings" and how to convert one into another?

While both this classes represent strings, they are still different, unrelated classes. They cannot be converted from one to another unless explicitly stated (like appropriate conversion operators or constructors), much like you cannot use simple assignment to convert std::vector<int> to std::list<int>.

QString has a static method fromStdString which can work for you:

name = QString.fromStdString(name1);

Notice that it expectes that std::string contains utf-8 data.

Unlike C#, Java and other languages, there is no native string type in the C++ language. When you type string literals like "abc" in source code, the compiler will convert them into an array of char type with a zero-value element at the end (forming the well-known 'zero-terminated string' values);

To make life easier for developers, C++ includes a standard library class for strings (std::string), but this is simply one implementation for handling zero-terminated character arrays. QString is another implementation. And there are, of course, hundreds more written by developers to suit their own needs.

So the reason you can't directly assign from one to the other is that they are completely different types - the compiler doesn't know that both are used for manipulating strings - it just treats them as separate, incompatible classes.

4

The difference is that they are, well, different. Unless the designers of QString provided an assignment operator taking a const std::string & argument, and they didn't do that.

To get a QString from std::string, you need to use QString::fromStdString, and the conversion is done assuming that std::string was UTF-8-encoded. You can't blindly assume that it's a valid assumption: you must understand where does your std::string come from and what encoding it's in.


You can't assign one to the other because it's not a cheap operation, and you'd need to provide some information about, at least, what is the encoding used in the std::string. Recall that QString stores UTF-16 Unicode code units. It doesn't store Unicode characters directly, and a QChar is really a misnomer: it's not a character, but a UTF-16 code unit (not code point). Sometimes, when a character happens to have a single-unit UTF-16 encoding, things work out "nicely", but that's but a coincidence.

std::string is worse: it knows nothing about its encoding, it's simply a vector of some characters. It makes it almost useless in many applications where you actually care what those characters mean.

QString is Qt frameworks's own implementation of Strings library whereas std::string is the implementation of Strings which comes bundled with rest of the other C/C++ libraries such as iostream, stdio etc. You can read up on QString further here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like