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]Load and display QString with proper encoding
Forum Updated to NodeBB v4.3 + New Features

[Solved]Load and display QString with proper encoding

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.1k 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
    SetBetterPass
    wrote on last edited by
    #1

    I am trying to load a name from file that has several special characters and if it is in file (looks like meno: Marek Ružička/) display it. Code here:
    @
    QFile File("info/"+meno+".txt");
    File.open(QIODevice::ReadOnly);
    QVariant Data(File.readAll());
    QString in = Data.toString(), pom;
    if(in.contains("meno:")){
    pom = in.split("meno:").at(1);
    pom=pom.split("/").at(0);
    ui->label_meno->setText(trUtf8("Celé meno: ")+pom);}@

    the part trUtf8("Celé meno: ") displays well but I cant find how to display string in pom, it alone looks like Marek RužiÄka, using toUtf8() function makes it Marek RuþiÃÂka, I've tried to convert it to stdString too but doesn't work either. I am not sure if the conversion from QFile to QVariant and to QString is right, if this causes problem how to read data properly?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi,
      First of all, why do you read in the data into a QVariant? File.ReadAll (recommended to use File.ReadLine()) give a QString, so no worry about UTF8 or other encoding there. Skip the QVariant!
      Second, the trUtf8 is obsolete!! Do not use this!! Use the tr() function for it
      So your code might look something like:
      @
      QFile File("Info/..");
      if( File.open(QIODevice::ReadOnly) == true) // Yes we could open the file
      {
      while (File.atEnd() == false)
      {
      QString In_str(File.readLine());
      if (In_str.contains("memo") == true)
      {
      QStringList Pom_strLst (In_str.split("memo"));
      // Oke, you do something to remove the "/' and keep the rest of the string. This is better done with QString functions like: chop etc!
      ui->label_memo=>setText(tr("Cele meno: "<<your QString here>>));
      }
      }
      @

      Greetz, Jeroen

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

        so I've solved it with this:
        @
        QFile File("info/"+meno+".txt");
        File.open(QIODevice::ReadOnly);
        QString in , pom;
        QTextCodec * cod = QTextCodec::codecForName("UTF-8");
        QByteArray data = File.readAll();
        in = cod->toUnicode(data);
        @
        QString in has now proper encoding and displays all characters properly.

        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