Results 1 to 2 of 2

Thread: Use QString::data() Pointer as Pointer to wchar_t/WCHAR

  1. #1
    Join Date
    Aug 2010
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default 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) ?

    Qt Code:
    1. static void toWideChar(const QString& qsIn, LPWSTR* wideCharOut)
    2. {
    3. Q_ASSERT(!qsIn.isEmpty());
    4. *wideCharOut = (LPWSTR)qsIn.data();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I found in qstring.h the ushort data pointer:

    Qt Code:
    1. struct Data {
    2. QBasicAtomicInt ref;
    3. int alloc, size;
    4. ushort *data;
    5. ushort clean : 1;
    6. ushort simpletext : 1;
    7. ushort righttoleft : 1;
    8. ushort asciiCache : 1;
    9. ushort capacity : 1;
    10. ushort reserved : 11;
    11. ushort array[1];
    12. };
    To copy to clipboard, switch view to plain text mode 

    This Pointer will be returned as a reinterpretet QChar Pointer:
    Qt Code:
    1. inline const QChar *QString::data() const
    2. { return reinterpret_cast<const QChar*>(d->data); }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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:
    Qt Code:
    1. QString str("Test String");
    2. WCHAR* wcar = (WCHAR*)(str.constData());
    3. std::wcout << wcar << '\n';
    To copy to clipboard, switch view to plain text mode 
    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.

  3. The following user says thank you to Zlatomir for this useful post:

    franku (4th December 2010)

Similar Threads

  1. QString -> wchar_t*
    By elizabeth.h1 in forum Qt Programming
    Replies: 0
    Last Post: 26th October 2009, 09:34
  2. attaching user data pointer to images in QTextEdit
    By Workeml in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2008, 20:06
  3. QString to wchar
    By rajveer in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 11:19
  4. WCHAR to QString giving error in vs2005
    By ucomesdag in forum Qt Programming
    Replies: 2
    Last Post: 1st May 2008, 23:25
  5. How to store/get pointer on QTreeWidgetItem data?
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2007, 22:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.