I want to read unicode from file and display the corresponding data in a QTextEdit.Please give me some suggestions.
Asked
Active
Viewed 884 times
-1
-
You are welcome to ask, but please read http://stackoverflow.com/help/on-topic – embert Mar 13 '14 at 06:38
-
Please provide us with more information so that we can tell you what's wrong in your program. Show us some parts of your code will be helpful – Tay2510 Mar 13 '14 at 07:16
-
I am unable to copy code,could you tell me how to copy my code here?? – QtUser Mar 13 '14 at 07:33
-
You shall not copy code if it is not a one liner into comments, but you can update your question... Anyway, my code works, so you could just take it as is. – László Papp Mar 13 '14 at 07:37
-
Yes,your code absolutely works fine for file contains normal data i.e english characters(eg:Hello),but what actually i need is i should get equvalent data from unicode characters(\u0c38\u0c60\u0c30\u0c31\u0c33\u0c2f\u0c38\u0c60\u0c30\u0c31\u0c33\u0c2f). – QtUser Mar 13 '14 at 08:13
1 Answers
1
Your question is a bit poor, but you need to use QFile and QTextEdit for this as follows:
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd())
myTextEdit.append(in.readLine());
or if you are not dealing with a huge file and small memory, you can read the file in as a whole without reading lines and chunks:
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
myTextEdit.setText(file.readAll());
// or setPlainText(file.readAll());
These will read the data in as unicode by default based on the documentation.
There are several ways of doing it, so this answer is just giving you some taste, and you will need to fine-tune this based on your specific scenario. You will need to add proper error handling, includes, build system files, etc.

László Papp
- 51,870
- 39
- 111
- 135
-
I tried this.But it is actually reading file data,what i need is to read unicode from file and display its corresponding text in textedit. – QtUser Mar 13 '14 at 06:46
-
@QtUser: you _are_ supposed to tell in the question what you have tried. Your question is currently very poor, I am afraid. Either way, look into QString::fromUtf8. – László Papp Mar 13 '14 at 06:48
-
At first, there's nothing like "simple" Unicode, but codecs based on Unicode, e.g. UTF-8, UTF-16 etc. You have to know which codec your text file use. Supposed your textfile is coded with UTF-8, you can use Laszlo's approach plus you have to set the codec in the textstream: in.setCodec("UTF-8"); – MichaelXanadu Mar 13 '14 at 08:49
-
@MichaelXanadu: yes, good point for encoding. QtUser, read [this](http://qt-project.org/doc/qt-5/qtextstream.html#setCodec-2). Although [auto-detection](http://qt-project.org/doc/qt-5/qtextstream.html#setAutoDetectUnicode) could be OK, too, in general. – László Papp Mar 13 '14 at 08:52
-
Thanks alot for your concern .I did two points what Laszlo and MichaelXanadu said,but did not work ,then i got other addition to my code i.e i forgot to set current language to my widget QTextEdit.Now my Application works fine..So,we need to set three things 1:QString::fromUtf8 2:in.setCodec("UTF-8") 3:setCurrentLanguage(Languageofunicode); :-) – QtUser Mar 14 '14 at 05:05
-
@QtUser: what do you mean by setCurrentLanguage? Which class contains that method? Also, please read [this](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – László Papp Mar 14 '14 at 06:32
-
@Laszlo Papp ,I gave clearly that this function is for QTextEdit class .And I will read whatever you suggested.. – QtUser Mar 14 '14 at 09:24
-
@QtUser: OK, can you provide a link to the documentation of that method? I do not see it [here](http://qt-project.org/doc/qt-5/qtextedit-members.html). – László Papp Mar 14 '14 at 09:43
-
Excuse me it is not setCurrentLanguage (),setCurrentFont() is the one i used , – QtUser Mar 18 '14 at 06:32