Results 1 to 3 of 3

Thread: Som problems about unicode,QString and QTextEdit

  1. #1
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Som problems about unicode,QString and QTextEdit

    I am really confused about the coding system.And I encounter a problem now.
    In the main() , I add this:
    Qt Code:
    1. QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
    2. QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    To copy to clipboard, switch view to plain text mode 
    My os is winxp,and I my program will deal with the Chinese;

    First,I am programming a program to encode and decode the DES.So I read the QString plaintext from the QTextEdit,and then I get its unicode by using QString::unicode(),convert the uniocde to 0-1 bits string manually,get the ciphertext by encoding the 0-1 string.Now the ciphertext is also 0-1 string,and I convert it into a unicode per 16 bits,for example,"110111100101110" is 28462.Finally,I will get an array of the unicode values(like 28462,38710,57196 and 46205).
    Then I convert it to QString using :
    Qt Code:
    1. s.append(QChar(unicode)); // this is a cycle,unicode is a variable: 28462,38710,57196 and 46205
    To copy to clipboard, switch view to plain text mode 

    Then I put the QString into a QTextEdit:
    Qt Code:
    1. qtextEdit->setText(s);
    To copy to clipboard, switch view to plain text mode 

    Surly it will display something unreadable.Then I get back the content from the qtextEdit:
    Qt Code:
    1. const QChar * data = qtextEdit->toPlainText().unicode();
    2. for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
    3. {
    4. qDebug() << data[i].unicode();
    5. }
    To copy to clipboard, switch view to plain text mode 

    However,it diaplays like this:
    Qt Code:
    1. 28462
    2. 56
    3. 32
    4. 10
    To copy to clipboard, switch view to plain text mode 

    I make some changes,but it always displays the wrong unicode values except the first one!
    What's wrong?How should I do?Thanks very much!


    Added after 1 4 minutes:


    Now I seem to know something!If I replace the following code:
    Qt Code:
    1. const QChar * data = qtextEdit->toPlainText().unicode();
    2. for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
    3. {
    4. qDebug() << data[i].unicode();
    5. }
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QString temp = qtextEdit->toPlainText();
    2. const QChar * data = temp.unicode();
    3. for(int i = 0; i < temp.size(); i++)
    4. {
    5. qDebug() << data[i].unicode();
    6. }
    To copy to clipboard, switch view to plain text mode 
    It works correctly!!!How strange it is!!!What is the difference between temp and qtextEdit->toPlainText()???Maybe it is something about the "implicit sharing"? I really hope that somebody can clasify this confusion!Thanks very much!
    Last edited by furskytl; 28th October 2011 at 04:51.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Som problems about unicode,QString and QTextEdit

    Works fine here, with no mangled characters (just placeholders where my fonts are deficient):
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. const unsigned short data[] = { 28462, 38710, 57196, 46205 };
    5. // const unsigned short data[] = { 74, 97, 107, 105, 347, 32, 116, 101, 107, 115, 116 };
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QString text;
    12. for (unsigned int i = 0; i < sizeof(data)/sizeof(unsigned short); ++i) {
    13. text += QChar( data[i] );
    14. }
    15. qDebug() << "Input:" << text;
    16.  
    17. t.show();
    18. t.setText(text);
    19.  
    20. QString result = t.toPlainText();
    21. qDebug() << "Output:" << result;
    22.  
    23. for (int i = 0; i < result.length(); ++i)
    24. qDebug() << result.at(i).unicode();
    25.  
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Now I seem to know something!If I replace the following code:
    Qt Code:
    1. const QChar * data = qtextEdit->toPlainText().unicode();
    2. for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
    3. {
    4. qDebug() << data[i].unicode();
    5. }
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. QString temp = qtextEdit->toPlainText();
    2. const QChar * data = temp.unicode();
    3. for(int i = 0; i < temp.size(); i++)
    4. {
    5. qDebug() << data[i].unicode();
    6. }
    To copy to clipboard, switch view to plain text mode 
    It works correctly!!!How strange it is!!!
    Not strange at all. In the first attempt you create a temporary QString (returned from toPlainString()) and get a pointer to its unicode buffer. The temporary object is destroyed at the end of the statement, so by the time you look at the buffer's content it could be filled with any old rubbish (and is) or crash your program. In the second you make a persistent copy of the temporary so that the unicode buffer persists.

    You can access the characters directly anyway (see my example).
    Last edited by ChrisW67; 28th October 2011 at 10:18.

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

    furskytl (28th October 2011)

  4. #3
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Som problems about unicode,QString and QTextEdit

    uh...Thanks for your help !

Similar Threads

  1. QString to unicode characters
    By jsmax in forum Newbie
    Replies: 1
    Last Post: 19th September 2011, 11:41
  2. QString to Unicode to char*
    By Daxos in forum Newbie
    Replies: 7
    Last Post: 4th November 2010, 18:56
  3. QString and Unicode
    By juanjo_de_la_pradera in forum Qt Programming
    Replies: 0
    Last Post: 23rd September 2010, 14:38
  4. QString unicode problem
    By kemp in forum Qt Programming
    Replies: 6
    Last Post: 21st September 2010, 16:11
  5. QString Unicode conversion
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 23:16

Tags for this Thread

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.