Use QString::data() Pointer as Pointer to wchar_t/WCHAR
Hi There, can comeone give me a hint on how to use the QString::data() ?
As I know from the Documentation Qt stores strings internally as Unicode. So my question is: Can I use the Pointer returned by data() as a pointer to WCHAR (wchar_t) ?
Code:
static void toWideChar(const QString& qsIn, LPWSTR* wideCharOut)
{
Q_ASSERT(!qsIn.isEmpty());
*wideCharOut = (LPWSTR)qsIn.data();
}
I found in qstring.h the ushort data pointer:
Code:
struct Data {
QBasicAtomicInt ref;
int alloc, size;
ushort *data;
ushort clean : 1;
ushort simpletext : 1;
ushort righttoleft : 1;
ushort asciiCache : 1;
ushort capacity : 1;
ushort reserved : 11;
ushort array[1];
};
This Pointer will be returned as a reinterpretet QChar Pointer:
Code:
{ return reinterpret_cast<const
QChar*>
(d
->data
);
}
All other attempts (i.e. toWCharArray()) seem to copy all the content and this is what I try to avoid.
Any Commtents (however, even good practive-hints) ? Thanks.
Re: Use QString::data() Pointer as Pointer to wchar_t/WCHAR
They both (WCHAR and QChar) are unsigned short integers (2 bytes), so this should work:
Code:
WCHAR* wcar = (WCHAR*)(str.constData());
std::wcout << wcar << '\n';
Quote:
For read-only access, constData() is faster because it never causes a deep copy to occur.
Quoted from here, so use constData member function.
Note that this pointer is valid only until you modify the original QString.