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
cang last edited by cang
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.
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.
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.
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?
cang last edited by
@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.
std::stringis good to keep pure ASCII.
QStringhowever, 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
QStringis 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.
cang last edited by
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.