Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

changing the application codec for all things



  • Hi,

    I am building a windows application that reads log files and search for the given parameters. The file I am working on produced by Windows-1254 encoding and it contains turkish chars.

    I can not display turkish chars in buttons labels etc. and I can not display the results correclty in my final data too.(which is a basic text that matches my parameters). What I understood is even tho my windows char set is windows-1254 my application is UTF-8. I want to change my application encoding completely to windows-1254. I tried almost everything in internet but no success yet..

    Btw I am using visual studio not qtCreator.

    Thank you for your answers.


  • Moderators

    I understand that you're parsing in a text file, showing the content in the UI, and then writing data in another file. So let's separate the matters.

    Parsing the File

    I assume you're parsing the file with QTextStream? By default QTextStream uses the locale of your current environment. But if you know that the file is in some other locale, you can force that by QTextStream::setCodec. By this you should be able to get the data into the program as UTF-16 (QString).

    Showing the Content

    There should be really no problem in showing Turkish Characters in your user interface.

    Writing the File

    Again, if you use QTextStream, you can force any kind of encoding with QTextStream::setCodec.

    @cang said in changing the application codec for all things:

    my application is UTF-8. I want to change my application encoding completely to windows-1254

    If you're talking about Qt 5 or Qt 6, it's always using Unicode internally. Strings are represented as UTF-16, to be precise. There's no way to change that.


  • Moderators

    @cang said in changing the application codec for all things:

    I want to change my application encoding completely to windows-1254.

    To clarify @kkoehne's point: You don't need to (and you cannot) change your application encoding to Windows-1254. Instead, you need to tell your text parser to decode your Windows-1254 log file. When you do this correctly, your application can display Turkish characters no problem. (It can even display Chinese, Arabic, Russian, etc. characters at the same time!)

    Can you show us your file-reading code?



  • @kkoehne actually I am showing the results in qTextEdit and parsing the file with an external json library and keeping my data in the memory. I am keeping the result in std::string before I pass it into qTextEdit->setText("myStdString"). In debugger my string looks fine with turkish chars but in qTextEdit its corrupt. Your solution is keeping my data in QString and decoding with utf16 if I understand correctly.

    @JKSH my file parser is very basic. I read my file with getLine and when It becomes a valid json object I am parsing it with rapidjson and then keeping this object in memory.


  • Lifetime Qt Champion

    Hi @cang,

    std::string is good to keep pure ASCII. QString however, is 16 bit Unicode and can handle the special non-ASCII characters. You can pass it to every Qt object without problems.

    You just need to make sure, that everything you convert to a QString is handled with correct encoding.

    I am parsing it with rapidjson and then keeping this object in memory.

    Any reason you don't use Qt JSON?

    By the way, typically JSON is in UTF-8 encoding. Using other encodings might give you extra work.

    Regards



  • @aha_1980 Hi,

    I passed my string into QString constructor like this "QtextEdit->setText(Qstring().fromLocal8bit(mystring.c_str()); and my problem solved. I believe this is the only way I can work with.

    About QtJson I never tried actually. I searched for the top 5 cpp json parser and benchmarked every one of them. Rapidjson was the fastest. My log files are like 5 gb per file and if you think QtJson is faster I can try.


Log in to reply