[SOLVED]Qt 5.7 string encoding issues
Hey mates
I read a value from database which contains special national characters,say "zażółć gęślą jaźń",and try to display it as a QString.
In old days Qt4 there were setCodecForCString() and setCodecForTr() whose worked well,sadly these are obsolete in Qt 5.7.
I've googled for a solution,but either found advices to use the above,obsolete functions,or some like QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("utf-8")),or ensure Qt Creator uses UTF-8 encoding.Unfortunately,nothing worked for me :/
Therefore I'm asking you for help,how nowadays in Qt 5.7 national characters should be handled properly in an application?
Re: Qt 5.7 string encoding issues
1. Which database ? What code page is used to connect to the database ?
2. Show some code.
Codec for CString and codec fo tr have nothing to read data from the database.
Re: Qt 5.7 string encoding issues
Code:
Generator
::Generator(const QString &databasePath
){
name = name.replace("\\","/");
database.setDatabaseName(name);
if(database.open())
{
Logger::info("Database " + databasePath + " opened successfuly.");
}
else
{
Logger::error("Couldn't open database " + databasePath + "! Error: " + database.lastError().text() + ". Application aborted.");
exit(-2);
}
}
Generator::~Generator()
{
if (database.isOpen())
{
database.close();
}
}
void Generator::generateDictionaryFile()
{
query.exec("select distinct ulica from AdresyKody_mid_mif");
Logger::info("Last error: " + query.lastError().text());
while(query.next())
{
Logger::info("Got from database: " + query.value(0).toString());//national characters here
}
}
But the issue is solved already.In other place I had code in Logger
Code:
logFile.write(logText.toLatin1());
and as it turned out that was the source of my issues.After change to:
Code:
logFile.write(logText.toUtf8());
all works fine.
Sorry mates for bothering you for nothing.