PDA

View Full Version : Problem with utf8



kbsk007
21st July 2018, 06:12
when I copy a QString with spanish words then I get difficulties with some letters:

As example, french is ok.
input: Il aura bientôt réglé ce poste.
output: Il aura bientôt réglé ce poste.

but spanish, not.
input: Su anulación llegó demasiado tarde.
output: Su anulaci�n lleg� demasiado tarde.

the code that I use is:

QStringList word = oneLine.split('#');

for (int i = 1; i < word.size(); ++i)
{
temp =word.at(i);
if(!temp.isEmpty())
output += temp+ '\n';
}

How I can copy the words correctly?
My os is kubuntu 18.04 and Qt 5.11, standard utf8
Thanks

Lesiok
21st July 2018, 08:14
1. What is oneLine ? How it is filled ?
2. What is temp and output ?

Show real code.

kbsk007
21st July 2018, 09:17
I include the cpp-file.


Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
inLabel = new QLabel(tr("Input: "));
inEdit = new QLineEdit(tr("/home/sp75/sp24.txt"));


outLabel = new QLabel("Output:");
outEdit = new QLineEdit("/home/sp75/sp24_mo1.txt");

QHBoxLayout *inLayout = new QHBoxLayout;
inLayout->addWidget(inLabel);
inLayout->addWidget(inEdit);

QHBoxLayout *outLayout = new QHBoxLayout;
outLayout->addWidget(reiheLabel);
outLayout->addWidget(reiheEdit);

showText = new QTextEdit;

QHBoxLayout *btnLayout = new QHBoxLayout;
untenLayout->addWidget(okButton);
untenLayout->addStretch();
untenLayout->addWidget(endButton);

connect(okButton, SIGNAL(clicked()), this, SLOT(loadTextFile()));
connect(endButton, SIGNAL(clicked()), this, SLOT(close()));

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(inLayout);
mainLayout->addLayout(outLayout);
mainLayout->addWidget(showText);
mainLayout->addLayout(btnLayout);
setLayout(mainLayout);
}

void Dialog::loadTextFile()
{
QString rowTxt, txtFileStr;
txtFileStr =inEdit->text();
QFile inputTxtFile(txtFileStr);

if(!inputTxtFile.open(QIODevice::ReadOnly))
qFatal( "Could not open the file" );

QTextStream inTxt(&inputTxtFile);
rowTxt.append(inTxt.readAll());
inputTxtFile.close();

QStringList lineTxt = rowTxt.split('\n');

QString outStr;
outStr = outEdit->text();
QFile outFile(outStr);
if(!outFile.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&outFile);

QString oneLine, word, temp;

for (int i = 0; i < lineTxt.size(); ++i){
lineZeile = lineTxt.at(i);
if (!lineZeile.isEmpty()){
oneLine = lineTxt.at(i);

QStringList word = oneLine.split('#');

for (int i = 1; i < word.size(); ++i)
{
temp =word.at(i);
if(!temp.isEmpty())
output += temp+ '\n';
}

}
}

showText->append(output);
showText->setUndoRedoEnabled(false);
showText->setUndoRedoEnabled(true);
out << output;
}


sp24.txt

#1 La impresión es hermosa. #2 Antes de empezar la impresión envÃ*en nos una prueba. #3 Adjunto le enviamos un artÃ*culo que seguramente le interesará.
#Su anulación llegó demasiado tarde.
#1 Me ha pagado sólo una pequeña parte a cuenta de su factura. #2 Pronto habrá terminado.

Lesiok
21st July 2018, 09:44
OK. And where is the problem : in showText or in out file ?
What is the code page of the sp24.txt file ?

kbsk007
21st July 2018, 10:11
The error is in both showText and outfile.
According to kubuntu the sp24.txt is utf8 coded.
It seems that QtCreator refuses to open the textfile when it is saved in different code.

Lesiok
21st July 2018, 10:37
Try to add this line :
inTxt.setCodec("UTF-8"); before readAll().
From QTextStream doc : By default, QTextCodec::codecForLocale() is used.

And please use [CODE] tag for code.

kbsk007
21st July 2018, 11:15
remains the same.

kbsk007
24th July 2018, 15:06
I found the error.
The text was written with kwrite, it seems that it is not reliable.
I copied the text with kate and the problems disappeared.
thanks