Results 1 to 8 of 8

Thread: Problem with utf8

  1. #1
    Join Date
    Aug 2015
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Problem with utf8

    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

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with utf8

    1. What is oneLine ? How it is filled ?
    2. What is temp and output ?

    Show real code.

  3. #3
    Join Date
    Aug 2015
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with utf8

    I include the cpp-file.
    Dialog:ialog(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.
    Last edited by kbsk007; 21st July 2018 at 10:25.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with utf8

    OK. And where is the problem : in showText or in out file ?
    What is the code page of the sp24.txt file ?

  5. #5
    Join Date
    Aug 2015
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with utf8

    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.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with utf8

    Try to add this line :
    Qt Code:
    1. inTxt.setCodec("UTF-8");
    To copy to clipboard, switch view to plain text mode 
    before readAll().
    From QTextStream doc : By default, QTextCodec::codecForLocale() is used.

    And please use [CODE] tag for code.

  7. #7
    Join Date
    Aug 2015
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with utf8

    remains the same.

  8. #8
    Join Date
    Aug 2015
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with utf8

    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

Similar Threads

  1. Replies: 1
    Last Post: 3rd May 2013, 17:07
  2. UTF8 and QStandardItem?
    By alexandernst in forum Newbie
    Replies: 17
    Last Post: 26th July 2009, 19:29
  3. UTF8 and ByteOrderMark (BOM)
    By SidGBF in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 23:25
  4. utf8 Problem (even in examples)
    By zamotf in forum Qt Programming
    Replies: 4
    Last Post: 28th December 2007, 08:47
  5. Utf8 problems
    By cristiano in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2006, 01:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.