PDA

View Full Version : Som problems about unicode,QString and QTextEdit



furskytl
28th October 2011, 03:51
I am really confused about the coding system.And I encounter a problem now.
In the main() , I add this:

QTextCodec::setCodecForCStrings(QTextCodec::codecF orLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForLoca le());
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 :

QString s;
s.append(QChar(unicode)); // this is a cycle,unicode is a variable: 28462,38710,57196 and 46205

Then I put the QString into a QTextEdit:

qtextEdit->setText(s);

Surly it will display something unreadable.Then I get back the content from the qtextEdit:

const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}

However,it diaplays like this:

28462
56
32
10

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:

const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
to

QString temp = qtextEdit->toPlainText();
const QChar * data = temp.unicode();
for(int i = 0; i < temp.size(); i++)
{
qDebug() << data[i].unicode();
}
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!

ChrisW67
28th October 2011, 09:12
Works fine here, with no mangled characters (just placeholders where my fonts are deficient):


#include <QtGui>
#include <QDebug>

const unsigned short data[] = { 28462, 38710, 57196, 46205 };
// const unsigned short data[] = { 74, 97, 107, 105, 347, 32, 116, 101, 107, 115, 116 };

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QString text;
for (unsigned int i = 0; i < sizeof(data)/sizeof(unsigned short); ++i) {
text += QChar( data[i] );
}
qDebug() << "Input:" << text;

QTextEdit t;
t.show();
t.setText(text);

QString result = t.toPlainText();
qDebug() << "Output:" << result;

for (int i = 0; i < result.length(); ++i)
qDebug() << result.at(i).unicode();

return app.exec();
}



Now I seem to know something!If I replace the following code:


const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}

to


QString temp = qtextEdit->toPlainText();
const QChar * data = temp.unicode();
for(int i = 0; i < temp.size(); i++)
{
qDebug() << data[i].unicode();
}
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).

furskytl
28th October 2011, 14:21
uh...Thanks for your help !