[vtk-developers] Converting vtkUnicodeString to QString

Timothy M. Shead tshead at sandia.gov
Mon Sep 14 11:38:36 EDT 2009


Jeff Baumes wrote:
> What is the correct way to convert a vtkUnicodeString to QString? I can do
> 
> QString qs = QString::fromUtf8(str.utf8_str());
>
> This also seems to work
> 
> QString qs = QString::fromUtf16(&str.utf16_str()[0]);
> 
> Are both equally valid, since the internal storage of vtkUnicodeString is UTF8 and for QString it is UTF16, so one conversion must take place either way?
> 
> Why does utf8_str() return a char* but utf16_str() return a vector?

Since vtkUnicodeString stores data using UTF-8 internally, utf8_str() 
can return a pointer to the internal storage.  Since utf16_str() must 
convert the data, it is returning a deep copy.

Note that since QString stores data using UTF-16, there's no way to 
avoid a deep copy, and either of your samples should yield roughly the 
same performance - the only difference is whether vtkUnicodeString or 
QString does the conversion.

Cheers,
Tim




More information about the vtk-developers mailing list