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]How to display strings in language other than english
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to display strings in language other than english

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 4.7k 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.
  • A Offline
    A Offline
    adnan
    wrote on 6 Oct 2012, 18:19 last edited by
    #1

    This program works fine, if i copy a string suppose in arabic "عدنان" in to textbox, pushing the button displays it in arabic in the label.
    @void MainWindow::on_pushButton_clicked()
    {
    ui->label->setText(ui->lineEdit->text());
    }@

    But if I do something like this:
    @void MainWindow::on_pushButton_clicked()
    {
    QString name("عدنان");
    ui->label->setText(name);
    }@

    It displays some garbled text!

    1 Reply Last reply
    0
    • U Offline
      U Offline
      uranusjr
      wrote on 6 Oct 2012, 22:24 last edited by
      #2

      This is because the implicit C string - QString conversion uses Latin-1 by default.

      There are many solutions to choose from:

      1. Internationalization

      Use Latin-character strings exclusively in your source, and use tr() to wrap string literals you wish to translate. Then, provide a translation file. This is best suited if you wish to (or plan to) make a multi-lingual application.

      See "documentation":http://doc.qt.digia.com/latest/internationalization.html

      1. Encode the C string explicitly

      Example:

      @
      /* This file should be saved with UTF-8 */
      #include <QApplication>
      #include <QLabel>
      #include <QTextCodec>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      QTextCodec *codec = QTextCodec::codecForName("UTF-8");

      QLabel l(codec->toUnicode("عدنان"));
      l.show();
      
      return a.exec&#40;&#41;;
      

      }
      @

      You might want to use this approach if your program is one-shot. But this really is not a good choice in most cases.

      3) Overwrite the default C string - QString conversion rule

      Usually an appropriate alternative to method 2. Example:

      @
      /* This file should be saved with UTF-8 */
      #include <QApplication>
      #include <QLabel>
      #include <QTextCodec>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

      QLabel l("عدنان");
      l.show();
      
      return a.exec&#40;&#41;;
      

      }
      @

      setCodecForCStrings() needs to be called only once in your application, and it overwrites all string conversion codec rule in the application.

      And you can use encodings other than UTF-8, of course. You just need to match the codec used with the encoding of your source file(s).

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on 6 Oct 2012, 22:34 last edited by
        #3

        [quote author="uranusjr" date="1349562294"]
        And you can use encodings other than UTF-8, of course. You just need to match the codec used with the encoding of your source file(s).[/quote]

        The full story is actually a bit more complicated as it involves the execution charset, not the source charset. But most of the times the mapping is 1:1.

        http://qt-project.org/wiki/Strings_and_encodings_in_Qt

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • E Offline
          E Offline
          elmigranto
          wrote on 6 Oct 2012, 23:54 last edited by
          #4

          QString::fromUtf8() saves the day. There is also QObject::trUtf8() if you are planning to internationalize. uranusjs's answer involving QTextCodec is also great.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on 7 Oct 2012, 04:26 last edited by
            #5

            Thanks a ton! It worked like a charm.
            @QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));@

            1 Reply Last reply
            0

            1/5

            6 Oct 2012, 18:19

            • Login

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