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. QString convert to LPCWSTR ?
Forum Updated to NodeBB v4.3 + New Features

QString convert to LPCWSTR ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.5k Views 2 Watching
  • 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    how QString convert to LPCWSTR ?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2
      QString foo("Hello world");
      auto bar = std::make_unique<wchar_t[]>(foo.length() + 1);
      foo.toWCharArray(bar.get()); //toWCharArray does not append null terminator
      bar[foo.length()] = 0; //so add it manually
      
      MessageBox(NULL, bar.get(), L"Some caption", MB_OK);
      

      or shorter:

      QString foo("Hello world");
      MessageBox(NULL, foo.toStdWString().c_str(), L"Some caption", MB_OK);
      

      but be aware that the pointer returned in the second example is temporary, so the above is ok, but this is wrong:

      QString foo("Hello world");
      LPCWSTR bar = foo.toStdWString().c_str();
      MessageBox(NULL, bar, L"Some caption", MB_OK); //bar is invalid at this point
      
      1 Reply Last reply
      0
      • O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        can i use:
        reinterpret_cast<LPCWSTR>(qstring.utf16()) ?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I personally cringe whenever I see reinterpret_cast but yes, it would work too.

          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