PDA

View Full Version : problem with accents



mmm286
5th August 2009, 07:49
Hi,

I have a file with encoding with UTF-8 Unicode text.

I read from file to insert the fields file in a qtablewidget. For do this I do:

QTextStream ins(&file);

while (!ins.atEnd()) {
QString line = ins.readLine();
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(line);
tabla->setItem(b,0,item);
tabla->setRowHeight(b,20);
b++;
}

Everything it's ok and the qtablewidget shows me the fields with accents.
But then I try to assign a variable to a cell selected with:

titulo= tabla->item(rowd,cold)->text();

And I see that this variable have strange characters instead of accents.
How could I solved that?

Many thanks and sorry for my english!

vcp
5th August 2009, 13:54
Hi,

I've a similar problem with QSqlQueryModel, and you can try this:


while (!ins.atEnd()) {
QString line = ins.readLine();

QString lineUTF8 = QString::fromUtf8(line.toByteArray());

QTableWidgetItem *item = new QTableWidgetItem;
item->setText(lineUTF8);
tabla->setItem(b,0,item);
tabla->setRowHeight(b,20);
b++;
}

mmm286
5th August 2009, 18:27
Hi,

I've a similar problem with QSqlQueryModel, and you can try this:


while (!ins.atEnd()) {
QString line = ins.readLine();

QString lineUTF8 = QString::fromUtf8(line.toByteArray());

QTableWidgetItem *item = new QTableWidgetItem;
item->setText(lineUTF8);
tabla->setItem(b,0,item);
tabla->setRowHeight(b,20);
b++;
}

It doesn't works it says me:

cpp400 : error: ‘class QString’ doen't have any member called ‘toByteArray’

Any help?
Many thanks!

Lesiok
5th August 2009, 18:45
You must set correct code for stream. Default codec is system locale dependent. So :


QTextStream ins(&file);
ins.setCodec("UTF-8");

while (!ins.atEnd()) {
QString line = ins.readLine();
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(line);
tabla->setItem(b,0,item);
tabla->setRowHeight(b,20);
b++;
}

mmm286
5th August 2009, 19:17
Many thanks!
There is no problem, the program runs ok. The problem is now that when I read from standard output of a process and put the text in a qtextbrowser, the text have strange characters instead of accents.

I do:



QByteArray result=cron->readAllStandardOutput();
QStringList lines = QString(result).split("\n");

foreach (QString line, lines) {

QMessageBox::information(this, "columna",line);
texto->append(line);
}

Any help?
Many thanks!