PDA

View Full Version : [SOLVED]Qt 5.7 string encoding issues



MasterBLB
4th August 2016, 08:47
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?

Lesiok
4th August 2016, 10:34
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.

MasterBLB
4th August 2016, 11:11
Generator::Generator(const QString &databasePath)
{
QString name = databasePath;
name = name.replace("\\","/");
database = QSqlDatabase::addDatabase("QSQLITE");
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()
{
QSqlQuery query(database);
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

logFile.write(logText.toLatin1());
and as it turned out that was the source of my issues.After change to:

logFile.write(logText.toUtf8());
all works fine.

Sorry mates for bothering you for nothing.