Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QLineEdit and QLabel displaying the wrong japanese characters
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QLineEdit and QLabel displaying the wrong japanese characters

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s3l3ctr
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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();
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        s3l3ctr
        wrote on last edited by
        #3

        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).

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved