[Qt-interest] QString constructor is not overloaded to take std::string as parameter
Nikos Chantziaras
realnc at arcor.de
Mon May 31 22:56:13 CEST 2010
On 05/31/2010 11:19 PM, Bahadır Doğan wrote:
> Hello
>
> I just wonder why QString class doesn't have a constructor taking
> std::string as parameter.
>[...]
> What is the reason of that?
I guess that's because they want you to go through QByteArray and
std::string::c_str() and std::string::data(). QString is Unicode,
std::string is not. Therefore, an std::string maps much better to a
QByteArray.
For "importing" directly into a QString, the static QString::from*()
routines should be used.
So I guess the question needs to be why QByteArray does not offer an
std::string constructor. Not sure. Might have something to do with
avoiding bloat since it's easy to use c_str() and data(). Or might have
something to do with binary compatibility between STL-enabled and
STL-disabled Qt builds. I think.
Two common ways of constructing a QByteArray from an std::string:
std::string stdStr;
QByteArray b1(stdStr.c_str());
QByteArray b2(stdStr.data(), stdStr.size());
The second one should be faster because it doesn't check for a
terminating '\0' on every character, since the length is known beforehand.
More information about the Qt-interest-old
mailing list