I am really confused about the coding system.And I encounter a problem now.
In the main() , I add this:
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
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 :
s.
append(QChar(unicode
));
// this is a cycle,unicode is a variable: 28462,38710,57196 and 46205
QString s;
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:
qtextEdit->setText(s);
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:
const QChar * data
= qtextEdit
->toPlainText
().
unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
To copy to clipboard, switch view to plain text mode
However,it diaplays like this:
28462
56
32
10
28462
56
32
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:
const QChar * data
= qtextEdit
->toPlainText
().
unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
const QChar * data = qtextEdit->toPlainText().unicode();
for(int i = 0; i < qtextEdit->toPlainText().size(); i++)
{
qDebug() << data[i].unicode();
}
To copy to clipboard, switch view to plain text mode
to
QString temp
= qtextEdit
->toPlainText
();
const QChar * data
= temp.
unicode();
for(int i = 0; i < temp.size(); i++)
{
qDebug() << data[i].unicode();
}
QString temp = qtextEdit->toPlainText();
const QChar * data = temp.unicode();
for(int i = 0; i < temp.size(); i++)
{
qDebug() << data[i].unicode();
}
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!
Bookmarks