PDA

View Full Version : Chowing Portuguese characters in a QTableView



graciano
24th May 2010, 13:35
Hi,
I want to display characters like { 'ç', 'á', 'ã', ...} in a column of a QTableView, used with a QSqlTableModel to access data from the database.
The original data was imported from a very large database and suffered some manipulation over a spreadsheet.
Any ideas?

Thanks

Lykurg
24th May 2010, 13:44
Sorry, but what is you problem. I don't get your point. Since Qt uses UTF-8 by default there is no problem if you set the right character set on your database.
So what have you tried or what is not exactly working?

graciano
24th May 2010, 14:06
This is the script used to create the database:


SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `localidade` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;
USE `localidade`;

-- -----------------------------------------------------
-- Table `localidade`.`Localidade`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `localidade`.`Localidade` ;

CREATE TABLE IF NOT EXISTS `localidade`.`Localidade` (
`idLocalidade` INT NOT NULL ,
`nomeLocalidade` VARCHAR(45) CHARACTER SET 'latin1' NOT NULL ,
PRIMARY KEY (`idLocalidade`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
ROW_FORMAT = DEFAULT;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;


If i insert data using the code i wrote and the Ui associated ... everything works fine.
The data inserted with lines like this ones:


QSqlQuery query;
query.prepare("INSERT INTO Localidade VALUES (3750, 'ÁGUEDA')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (1000, 'LISBOA')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (1049, 'LISBOA')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (1050, 'LISBOA')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2000, 'PÓVOA DE SANTARÉM')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2000, 'SANTARÉM')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2000, 'SÃO VICENTE DO PAUL')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2005, 'AZOIA DE BAIXO')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2005, 'PÓVOA DA ISENTA')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2025, 'ABRÃ')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2025, 'GANÇARIA')"); query.exec();
query.prepare("INSERT INTO Localidade VALUES (2025, 'TREMÊS')"); query.exec();

query.prepare("INSERT INTO Localidade VALUES (2090, 'ALPIARÇA')"); query.exec();

... fails to be displayed correctly.

Guess i could i write this lines to force the second column to be an UTF8 ... ?

graciano
25th May 2010, 16:03
Well ... this only worked for me by creating a function to read the text from a csv file and adding the data to the model:


void LocalidadeDialog::inserirLocalidadesNacionais()
{
QFile ficheiro(":/ctt/ctt");
if (!ficheiro.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&ficheiro);
QString linha;
QString col1, col2;
int separador;
QSqlRecord registo;
QSqlField c1("idLocalidade", QVariant::Int);
QSqlField c2("nomeLocalidade", QVariant::String);
registo.append(c1);
registo.append(c2);
while (!in.atEnd())
{
linha = in.readLine();
separador = linha.indexOf(",");
col1 = linha.mid(0, separador);
col2 = linha.mid(separador+ 1);
registo.setValue("idLocalidade", col1);
registo.setValue("nomeLocalidade", col2);
modeloLocalidade->insertRecord(-1, registo);
modeloLocalidade->select(); // OK
}
//modeloLocalidade->select(); //not OK
}


Why is that line 24 works and if i do the same at line 26 (outside the loop) i get lots of blank lines if the insert fails (duplicate keys in the database)?

By the way ... what showld i use to create some "entertainment" while the data is being inserted into the database, assuming that i want no interaction user/application?

Thanks

Lykurg
25th May 2010, 16:12
Hi,

you set utf8 global but in your table Localidade you use latin1 as character set. You might want change that. Further make sure your cpp file is saved as utf8! That is possible the reason why inserting work with a file and not in your code. Also try to use QString::fromUtf8() for your inserts.

You also can set the default character set after establishing the connection to the database with:SET NAMES 'utf8';
SET CHARACTER SET 'utf8';

graciano
25th May 2010, 16:21
Hi,

you set utf8 global but in your table Localidade you use latin1 as character set. You might want change that.

I had noticed that just afted i posted it:o

You might just be right about saving the file in utf8.

Any way ... it was not a good idea to make the insertion depend on my cpp!

About the other subject ... a progress bar?!

Lykurg
25th May 2010, 21:17
About the other subject ... a progress bar?!why not. But how long will it take? If it's only a couple of seconds, I would use a simple message that things currently imported. And the common "circling dot's" for indicating a process.