Important: Please read the Qt Code of Conduct -

Correct way to convert a QString to a const wchar_t* with L prefix

  • Hello,

    I try to convert a QString to a const wchar_t* with L prefix.

    Is the following try correct?

    void qStringToWChar(QString string)
        //Required is a const wchar_t* from a QString (Output shall be like: const wchar_t* array = L"string_content";
        const wchar_t * array;
        array = new wchar_t[string.length() + 1];
        array[string.length()] = 0;
        qDebug() << "Output as wchar: " << array;
        delete[] array;
    I am wondering whether the "L" before the string needs to be included somewhere.
    The system is windows 10 x64.
    Thank you very much :-)

  • Lifetime Qt Champion

    Hi @robro,

    Indeed the L prefix is only needed if you specify the constant in source code. Your code looks good so far.

    However if you only need a read access to the QString contents and only need that on Windows, then maybe you don't need to copy the data:

    const wchar_t *array = (const wchar_t *)string.utf16();

    Note that this only works if you don't modify the string while using the array pointer.


  • Thank you very much! :-)
    The read access is sufficient for me, so I will take your example.

    I thought it would work, but it seems I have another problem.

    I am using an API which has the data type SI_WC (wchar_t).

    If I try that:

    SI_WC myConvertedString[] = (SI_WC[])myQString.utf16();

    Then I get the error: C2440 const ushort* cannot be converted to SI_WC[].

    How can this be solved?


  • @robro
    I don't know whether your intention was to copy the string to an array, but you can't do that with a cast. And QString::utf16() returns a const pointer. So I'm guessing you want:

    const SI_WC *myConvertedString = (const SI_WC *)myQString.utf16();

    You might not (depending on the definition of wchar_t) need the cast, i.e.:

    const SI_WC *myConvertedString = myQString.utf16();

    may work.

    But from the name of the variable, are you wanting actually to copy that string into a buffer where you're going to "convert" (alter) it, or what?

    I am wondering: are you really wanting to use by any chance?

  • Lifetime Qt Champion

    Hi @robro,

    what is the definition of SI_WC? It needs to be compatible to wchar_t aka unsigned short (on Windows, Unix also has a wchar_t which is 4 byte).

    Two further notes:

    • in C [] and * are interchangeable, so your code could be written as const SI_WC *myConvertedString = (const SI_WC *)myQString.utf16();
    • if a function takes a non const pointer, it is not save to give a const pointer, as utf16() does. The function may want to modify the data given by the pointer (called pass by reference)

  • Works fine! :-)
    Thanks a lot!

  • Here are a couple of variations that worked for me in Visual Studio 2017:

    diac = QStringLiteral("ÀÉ");
    uint chin[] = {0x4E83, 0};        // L'亃'
    diac2 = QString::fromUcs4(chin);	
    wchar_t chinW[] = {L'亃', 0};
    wchar_t *chinW2 = L"亃";
    diac3 = QString::fromWCharArray(chinW2);   // also chinW
    diac += diac2 + diac3;

    Sorry I just realized you want to go the other way.

Log in to reply