PDA

View Full Version : Problems with Unicode(UTF8)



cristiano
4th December 2006, 00:39
Hello,

I am with serious problems with Internacionalization (UNICODE) of characters in the Qt with the database PostgreSQL.

The data with caracters accented, appear wrong and also they are inserted in the DB of incorrect form.

I am running PosgreSQL on of the FreeBSD. Already I modified the configuration archive "postgresql.conf" for "client_encoding = utf8" more I did not get success.

I tried to create the base as UTF8 using,

$ createdb -E UTF8 foobase;

More simply when I create the base with UNICODE is shown the data for the Qt.

jacek
4th December 2006, 00:52
When Qt connects to PostgreSQL it sets the proper client encoding, so you don't have to worry about the database encoding --- it should work out of the box.

How do you retrieve strings from the database?

cristiano
4th December 2006, 01:46
For example, selected the data in a QComboBox, the data appear incorrect.

QSqlQuery queryBairroLogradouro( "SELECT nm_bairro FROM bairros ORDER BY nm_bairro;" );

cmbBairroLogradouro->clear();
while ( queryBairroLogradouro.next() )
{
cmbBairroLogradouro->insertItem( queryBairroLogradouro.value( 0 ).toString() );
}

In form caracters appears as well as in the image in annex. Where the correct word would have to be in Portuguese “ANTENDIMENTO PRÉ-HOSPITALAR” with accent in the letter “É”

jacek
4th December 2006, 01:54
For example, selected the data in a QComboBox, the data appear incorrect.
This looks OK. How do you put data into the database?

cristiano
4th December 2006, 02:58
Jacek,

I use the (QSqlRecord*) to make insert in the database, the code is here:

http://200.193.29.195/trolltech/code1.h.html

The ui Form is here:

http://200.193.29.195/trolltech/gui.png

jacek
4th December 2006, 14:58
QString Test = textEditDescricao->text();
textEditDescricao->setText(QString::fromUtf8(Test));
You shouldn't do this, except for that everything looks OK.

Does your application update records from "bairros" table? Maybe you should check whether data in the database is correctly encoded? Do you see correct characters if you start psql, issue "SET client_encoding TO '<encoding_used_by_your_system>';" and select something from that table?

cristiano
4th December 2006, 16:08
In the terminal "KDE" of the system it correctly appears the data. it look register 4

footest=# select * from bairros;
id_bairro | id_cidade | nm_bairro | ch_repositorio
-----------+-----------+----------------+----------------
1 | 8105 | ESTREITO | S
2 | 8327 | PRAIA COMPRIDA | S
3 | 8105 | CENTRO | S
4 | 8105 | ATENÇÃO | S
(4 registros)

"ATENÇÃO" correct !

In Qt the characters appear wrong.

jacek
4th December 2006, 16:44
"ATENÇÃO" correct !
Did you use "SET client_encoding TO" to set the encoding explicitly?

cristiano
4th December 2006, 18:07
For this in case that not, I am only using "setlocale" of "Shell" that he is configured for ptBR.

LC_ALL=pt_BR.ISO8859-1

jacek
4th December 2006, 18:22
For this in case that not, I am only using "setlocale" of "Shell" that he is configured for ptBR.
If you didn't set the encoding using SET client_encoding, then we still don't know if database contents is correctly encoded.

Alternatively you can try:

footest=# show client_encoding;
footest=# select * from bairros;
If you see correct characters and client_encoding has the right value, then the database is OK.

cristiano
4th December 2006, 19:23
This is returned,

footest=# show client_encoding;
client_encoding
-----------------
SQL_ASCII
(1 registro)

footest=# select * from bairros;
id_bairro | id_cidade | nm_bairro | ch_repositorio
-----------+-----------+----------------+----------------
1 | 8105 | ESTREITO | S
2 | 8327 | PRAIA COMPRIDA | S
3 | 8105 | CENTRO | S
4 | 8105 | ATENÇÃO | S
(4 registros)

jacek
4th December 2006, 20:41
OK and what happens when you execute this?
footest=# set client_encoding to 'latin1';
footest=# select * from bairros;

cristiano
4th December 2006, 23:37
footest=# set client_encoding to 'latin1';
SET
footest=# select * from bairros;
id_bairro | id_cidade | nm_bairro | ch_repositorio
-----------+-----------+----------------+----------------
1 | 8105 | ESTREITO | S
2 | 8327 | PRAIA COMPRIDA | S
3 | 8105 | CENTRO | S
4 | 8105 | ATENÇÃO | S
(4 registros)

footest=# show client_encoding;
client_encoding
-----------------
latin1
(1 registro)

As 'latin1' the data is shown correctly, more in the continuous application with the incorrect characters.

jacek
5th December 2006, 00:06
Hmm... everything looks OK. Does the database protest if you issue:

SET client_encoding TO 'UNICODE';?

cristiano
5th December 2006, 00:27
SET client_encoding TO 'UNICODE';
SET
footest=# select * from bairros;
id_bairro | id_cidade | nm_bairro | ch_repositorio
-----------+-----------+----------------+----------------
1 | 8105 | ESTREITO | S
2 | 8327 | PRAIA COMPRIDA | S
3 | 8105 | CENTRO | S
4 | 8105 | ATENO | S
(4 registros)

The data come incorrect, already I am losing the hope for this.

jacek
5th December 2006, 12:33
SET client_encoding TO 'UNICODE';
SET
OK, so IMO there shouldn't be any problem with the database.

What does this program output?

#include <QCoreApplication>
#include <QTextCodec>

#include <QtDebug>

int main( int argc, char ** argv )
{
QCoreApplication app( argc, argv );
qDebug() << QTextCodec::codecForLocale()->name();
}

Do you use QString::fromUtf8(), QString::toUtf8() and similar methods in your program?
Do you use any static or global variables that have something to do with the database handling?


The data come incorrect
It doesn't matter, the most important part is that PostgreSQL wants to talk with you in Unicode.