[SOLVED] QLineEdit and QLabel displaying the wrong japanese characters



  • Hello

    What i'm doing is, opening a file that may contain unicode characters, then i read all the text from that file to a QString, then with some regexp i search for the data i'm interested in.
    Now, the problem is, QLineEdit isn't displaying the correct text. By correct i mean i get a different unicode characters from what i'm expecting. It does the same with QLabel too. I tried to save back the read content to a text file to check whether it is wrong there or not, but it's ok that way. The QRegularExpressionMatch is ok too, because i saved the result directly into a text file and the result was good.

    Some code
    @
    QString fileName = QFileDialog::getOpenFileName(0,tr("Open .osu file"),"../../","*.osu");
    if(!fileName.isEmpty()){
    ui->ucOsuFileNameText->setText(fileName);
    QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QTextStream stream(&file);
    QString fileContent = stream.readAll();
    QRegularExpression regexp;
    QRegularExpressionMatch regexpResult;
    QStringList regexpList;
    regexpList.append(_Scanner::artistRegexp);
    regexpList.append(_Scanner::titleRegexp);
    for(int i=0; i<regexpList.length(); ++i){
    regexp.setPattern(regexpList[i]);
    regexpResult = regexp.match(fileContent);
    qDebug() << fileContent.length();
    switch(i){
    case 0: ui->ucArtistText->setText(regexpResult.capturedTexts().last());
    break;
    case 1: ui->ucTitleText->setText(regexpResult.capturedTexts().last());
    break;
    }
    }
    file.close();
    }
    @

    What am i doing wrong? I read after the translations and codecs but i really didn't understand that, but i think, as QString supports unicode by default, it doesn't require any conversions.

    Thank you for your answers.


  • Moderators

    Which encoding does the file use ? QTextStream can only automatically detect BOM for UTF-16 and UTF-32. If your file is something like UTF-8 then you need to set that codec on the stream before calling readAll() eg.
    @
    QTextStream stream(&file);
    stream.setCodec("UTF-8");
    QString fileContent = stream.readAll();
    @



  • Thank you for the quick reply and also for the solution :) .
    I set the codec as you mentioned and it worked, the file is in UTF-8 without BOM (as notepad++ says).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.