[Interest] Converting std::string.c_str() code to use QString methods

Constantin Makshin cmakshin at gmail.com
Fri Aug 30 21:42:47 CEST 2013


According to the documentation, toStdString() converts characters using
toAscii(). That means non-ASCII characters (ones with codes above 127)
will get messed up unless you explicitly set codec by calling
QTextCodec::setCodecForCStrings().

On 08/30/2013 11:33 PM, Michael Jackson wrote:
> So maybe I should go with .toStdString().c_str() to remain as compatible as possible.
> 
> --
> MJ.
> 
> On Aug 30, 2013, at 3:26 PM, Constantin Makshin <cmakshin at gmail.com> wrote:
> 
>> std::string is more like QByteArray than QString because it's just a
>> dynamically-resizeable version of classic C strings, i.e. it doesn't
>> know anything about encodings. On the other hand, internally QString
>> always is UTF-16 and therefore knows how to convert its contents to
>> other encodings.
>>
>> That means there's no single answer to your question.
>> As for your example, on *nix toUtf8().constData() will likely do the
>> trick, on Windows it's better to use utf16() together with _wfopen().
>>
>> On 08/30/2013 11:08 PM, Michael Jackson wrote:
>>> I have a large code base that we are migrating to more fully utilize Qt classes. Many places in the code have lines like the following:
>>>
>>> std::string path("/path/to/foo.txt");
>>> FILE* f = fopen(path.c_str(), "wb");
>>>
>>> If the "path" variable is now declared as a QString which QString method would be me the equivalent as the "c_str()" of std::string?
>>>
>>> Not sure if I should be using "toASCII()" or "data()" or "toUtf8()" or what exactly.
>>>
>>> Google didn't really give me a definitive answer.
>>>
>>> Many Thanks
>>> --
>>> Mike Jackson
>>> imikejackson & gmail * com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130830/504375c2/attachment.sig>


More information about the Interest mailing list