Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Correct way to convert a QString to a const wchar_t* with L prefix
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
qstring
7 Posts 4 Posters 5.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    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];
        string.toWCharArray(array);
        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 :-)
    
    aha_1980A 1 Reply Last reply
    0
    • R robro

      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];
          string.toWCharArray(array);
          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 :-)
      
      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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:

      http://doc.qt.io/qt-5/qstring.html#utf16

      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.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • R Offline
        R Offline
        robro
        wrote on last edited by robro
        #3

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

        EDIT:
        @aha_1980
        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?

        Thanks!

        JonBJ aha_1980A 2 Replies Last reply
        0
        • R robro

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

          EDIT:
          @aha_1980
          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?

          Thanks!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 http://doc.qt.io/qt-5/qstring.html#toWCharArray by any chance?

          1 Reply Last reply
          1
          • R robro

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

            EDIT:
            @aha_1980
            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?

            Thanks!

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #5

            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)

            Qt has to stay free or it will die.

            1 Reply Last reply
            2
            • R Offline
              R Offline
              robro
              wrote on last edited by
              #6

              Works fine! :-)
              Thanks a lot!

              1 Reply Last reply
              0
              • SprezzaturaS Offline
                SprezzaturaS Offline
                Sprezzatura
                wrote on last edited by Sprezzatura
                #7

                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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved