[Interest] Is it safe to use QString::utf16() as a Windows wchar_t*?

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Thu Nov 9 13:56:16 CET 2017


Il 09/11/2017 10:15, Nikos Chantziaras ha scritto:
> This saves an allocation, a copy and a free since I don't have to use
> QString::toWCharArray(). However, is this actually safe? "It seems to
> work," and AFAIK, QString::d->data() is already in the format and
> encoding Windows excepts a wchar_t* to be. Is this correct?

It is safe. QString is UTF-16 encoded (same code unit size as for 
wchar_t on Windows), and Windows APIs accepting wchar_t expect a 
NUL-terminated UTF-16 string as a wchar_t*, which is exactly what 
utf16() provides.

See also various of such usages into Qt itself, e.g.

> https://code.woboq.org/qt5/qtbase/src/corelib/io/qfilesystemengine_win.cpp.html#387


Cheers,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (UK) Ltd., a KDAB Group company | Tel: UK +44-1625-809908
KDAB - Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4007 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171109/893cf1ad/attachment.bin>


More information about the Interest mailing list