PDA

View Full Version : Qt creator not showing Arabic alphabets.



toufic.dbouk
4th September 2013, 15:41
Hello friends,
im working on a Qt app where i have to use Arabic alphabets in the combobox.
i add the alphabets as follows :

ui->symb_combo->addItem("أدخل الرمز");
i have a problem , when i read the symbol selected from that combo box and try to print it
it doesnt show. instead of showing : أدخل الرمز it shows : ???? ?????
so as you can see ,it is showing " ?? " instead on Arabic alphabets
any help is appreciated to solve this problem.
Thanks in advance.

wysota
4th September 2013, 15:49
QtCreator has nothing to do with this. You just didn't inform QString how to convert your input to Unicode. What encoding does your source code use?

toufic.dbouk
4th September 2013, 17:31
Thanks for your reply,
but excuse me for this question , what do you exactly mean by What encoding does your source code use? ?
how do i know that ?

if i open the .cpp file in notepad and check the encoding , it shows UTF-8( if thats what you mean ).
im just adding أدخل الرمز to my combo box and then retrieving and printing


ui->symb_combo->addItem("أدخل الرمز");

void method()
{
QString symb;
symb = ui->symb_combo->currentText();
qDebug () << ""+symb;
}

but i get ?? instead of the symbol

Added after 1 28 minutes:

Still waiting for help guys
i saw a thread stating a solution for this problem but unfortunately it didn't work...
it consisted of 2 steps:
1st : save the source file as UTF-8 encoding
2nd : inform Qt creator of this by adding this line of code to the main.cpp

QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8"));
is the * QTextCodec::setCodecForCStrings * method usable ? does it exist in Qt 5.1 ?

toufic.dbouk
4th September 2013, 18:41
Hello again,
im still trying to figure it out...
Another approach seems to be adding this line:

QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("CP1256") );
CP1256 is the unicode encoding for Arabic
but unfortunately it didn't work...
i appropriate any help guys.

wysota
4th September 2013, 19:18
QString str = QString::fromUtf8("your arabic text goes here");

BTW. CP1256 is not any "unicode encoding" for arabic.

toufic.dbouk
4th September 2013, 19:30
hey wysota,
i tried this before posting a new thread on the forum

QString str = QString::fromUtf8("your arabic text goes here");
this doesnt seem to solve my problem
any other ideas ?
and what is CP1256 ? something that helps in converting from non unicode to unicode ?

Just to make it clear if its not, my Arabic text appears in the combo box, but when i retrieve it print it ( as mentioned in the above posts) it gives *??* instead.

example:

QString str = QString::fromUtf8("أدخل الرمز");
ui->Symb_combo->addItem(str);
void print() {
QString symb;
symb = ui->Symb_combo->currentText();
qDebug () << ""+symb;
}

this reults in : "???? ?????"

wysota
4th September 2013, 19:49
hey wysota,
i tried this before posting a new thread on the forum

QString str = QString::fromUtf8("your arabic text goes here");
this doesnt seem to solve my problem
In that case you're not using UTF-8 encoding. You should get somewhat more familiar with your system and learn what encoding it uses.


and what is CP1256 ? something that helps in converting from non unicode to unicode ?
http://lmgtfy.com?q=CP1256


Just to make it clear if its not, my Arabic text appears in the combo box, but when i retrieve it print it ( as mentioned in the above posts) it gives *??* instead.
Your console doesn't know how to print it, you have to convert it to something it understands (which brings us back to the original problem of learning what encoding the system uses).

toufic.dbouk
4th September 2013, 19:58
alright thank you Wysota
but can you tell me how to find what encoding does my system use ?
when i open the .cpp file in notepad it shows UTF-8 encoding.
Even if i did that through Qt creator ( edit > select encoding >UTF-8 ) i still get the same problem.
i really need help here
Thanks for all your replies Wysota.

wysota
4th September 2013, 20:29
but can you tell me how to find what encoding does my system use ?
Consult your operating system manual or contact technical service of the system vendor.

toufic.dbouk
4th September 2013, 20:36
sounds complicated but okay ill do that.
but you can explain to me why does the Arabic text appears in the combo box fields but doesn't appear while printing it ?
this is really confusing , and im saving the .cpp file( source code that contains the Arabic text as UTF-8 ) so i guess that
should make the encoding of this file as UTF-8 and it stills gives *??? * when printing the text in the combo box.
i wish i wasted all this time on a technical problem instead of such problem....
thanks for your help and please let me know if you have any other ideas to try , i really tried more than the 3 trails mentioned in this post.

wysota
4th September 2013, 20:44
but you can explain to me why does the Arabic text appears in the combo box fields but doesn't appear while printing it ?
Because your GUI (Qt) uses Unicode and your console doesn't.


this is really confusing , and im saving the .cpp file( source code that contains the Arabic text as UTF-8 )
Encoding of the source file has nothing to do with this. Even if you encoded your file in ISO8859-2, your console would still not understand unicode nor the default conversion qDebug() uses to print strings.


i wish i wasted all this time on a technical problem instead of such problem....
This is a technical problem. The problem is that you don't know how your system works. This problem is completely unrelated to Qt, it only manifests itself while using Qt. You'd still have this same problem if you wanted to dump any text encoded in encoding A to a console (or other target) that uses a different encoding.


thanks for your help and please let me know if you have any other ideas to try , i really tried more than the 3 trails mentioned in this post.

Ask google about "default encoding for Arabic windows system" as I'm assuming we're talking about Windows. Then ask QTextCodec nicely to convert the given unicode text to this encoding before dumping it to the console. A good initial guess if you can't use web search engines is to try the code page you already mentioned in this thread.

toufic.dbouk
4th September 2013, 20:56
Hi Wysota ,

i don't think we are on the same page...
im compiling and ruining the app in debug mode ,
and im printing it using qDebug() to show in the Application Output in Qt creator ( thats where the ?? are shown )
should i try it on the cmd console ? or it wont matter ?

since your first reply to this post was , what is your source code unicode , i assumed to try and change it and see what happens
if i change the source code unicode through notepad, the Arabic text wont appear even in the combo box.

im not familiar with all this encoding stuff , but ill look further into this after solving my problem cuz i really have to go through this application ASAP.


Ask google about "default encoding for Arabic windows system" as I'm assuming we're talking about Windows. Then ask QTextCodec nicely to convert the given unicode text to this encoding before dumping it to the console.
ill google this and see what happens...

Best Regards Wysota.

wysota
4th September 2013, 21:12
i don't think we are on the same page...
im compiling and ruining the app in debug mode ,
and im printing it using qDebug() to show in the Application Output in Qt creator ( thats where the ?? are shown )
should i try it on the cmd console ? or it wont matter ?
You can do whatever you want. I don't care really. I already told you that the problem is text encoding. I know that the source encoding in your case is Unicode, I have no idea what the target encoding on your system is. There is nothing more I can do for you apart repeating that you need to output textual content from qDebug in an encoding your target device understands. Whether it is the pipe Qt Creator opens to your application, the cmd application or anything else, is a secondary issue.


since your first reply to this post was , what is your source code unicode , i assumed to try and change it and see what happens
My first reply was assuming that the text you get in your UI is wrong. You can ignore everything I said prior to you saying you had a problem with qDebug.


im not familiar with all this encoding stuff
Yeah, well... if you are/want to be a software developer then you should get familiar with "this encoding stuff". Especially if you're coming from a country that uses an alphabet different than American English is using.

toufic.dbouk
4th September 2013, 22:08
Thanks , ill try to figure it out and get back to this ost to mark it as SOLVED

Added after 44 minutes:

Hey Wysota ,
just wanna update you with everyone who is viewing this post that i showed the result instead of qDebug() in a table view ,
and it actually showed the Arabic alphabetic.

if this changes anything with the previous discussions
Best Regards.

toufic.dbouk
7th September 2013, 23:15
one thing that might be helpful in this post is trying the flowing :
1st: save all source files as UTF-8 encoding
2nd: tools -> options -> text editor -> behavior -> set default encoding to UTF-8

but still when using the qDebug () it shows : ??? instead of Arabic letters
but at least now if you retrieve the data ina table view , Arabic letters will appear
if anyone knows how to solve the qDebug() issue please let me know

Thanks.

ahmedb
7th November 2013, 14:15
Qt requires that the encoding of the code files ( .h, .cpp ) to be the same as the "codecForCStrings" to be able to read the Arabic text in the correct manner.

so, I recommend that you put the following line of code in the main() function (after) initializing QApplication object and (before) showing any widgets/windows:



QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8") );


that's if the encoding of your files are UTF-8

but if they are set to the default encoding of the system, which is "windows-1256" in windows-xp, i think, then you should write it:



QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("windows-1256") );

toufic.dbouk
7th November 2013, 23:54
@ahmedb
Check the first 3-4 posts in this thread , we already went through your suggestion.
Thanks for your concern.

ChrisW67
8th November 2013, 01:08
if anyone knows how to solve the qDebug() issue please let me know
There is no qDebug() issue, it simply outputs what you tell it to using the default locale encoding for QStrings. If your console, i.e. the place your output appears, does not understand the encoding used then that is not the fault of qDebug.

This example Qt4 program:


#include <QtCore>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QString test = QString::fromUtf8("\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u 0668\u0669");
qDebug() << test;
return 0;
}

Outputs "٠١٢٣٤٥٦٧٨٩" just fine on my UTF-8 enabled Linux console because the the default codec works well.

My US English Windows XP machine is expecting a different encoding and outputs this by default:


Y:\tt>debug\tt.exe
"??????????"

where the '?' stands in for a unicode code point that has no equivalent in the Windows 8-bit code page.
The behaviour of the Windows console is variable beast that depends on selected fonts and other settings etc. You can try this... Start a CMD shell, ensure its font is set to "Lucida Console" or "Consolas", execute "chcp 65001", then run your program from the command line and see if the output is better.

toufic.dbouk
9th November 2013, 11:21
@ChrisW67
i tried the example you mentioned above :

QString test = QString::fromUtf8("\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u 0668\u0669");
qDebug() << test;
i get this warning : character represented by universal-character-name '\u0669' cannot be represented in the current code page (1252)

tried setting the encoding to UTF-8 and to windows-1252
but still all i get in the Qt Application Output Console is : "??????????"


then run your program from the command line and see if the output is better
How to do that ? locate the test.exe file generated by Qt and execute it ? or do i have to run qmake on the .pro file ? build and compile then run ?