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. Displaying a UTF-8 and UTF-16 text in QTextEdit
Forum Update on Monday, May 27th 2025

Displaying a UTF-8 and UTF-16 text in QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.5k Views
  • 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.
  • Z Offline
    Z Offline
    Zunneh
    wrote on last edited by
    #1

    Hello all,
    i want to make a little program to convert String to UTF-8 or UTF-16 and then displaying it in QTextEdit,
    However, everyTime i want to show the QByteArray in QTextEdit, the data is implicitly converted to QString,
    For exemple when i write "hello world" , it must display "\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64" but the data is implicitly converted and i get "hello world" again in my QTextEdit :

    This is my code,

    QString textFrom = ui->TXT_From->toPainText();
    QTextCodec *codec = 0;
    QByteArray encodedString;
    QString DataAsString;
    
    switch (ui->CMB_From->currentIndex()) {
        case  TextType::UTF8:
            qDebug() <<"From UTF8";
            codec = QTextCodec::codecForName("UTF-8");
            encodedString = codec->fromUnicode(textFrom);
    
            ui->TXT_To->setText(encodedString);
    

    lCapture d’écran 2020-06-14 à 17.34.22.png

    Thanks

    my english is average, please use simple words and try to be the most explicit, thank you

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      If you want to show the bytes of a string instead the string you have to convert every char into it's bytes and convert them to printable chars (in your case it looks like you want to display the hex values). See QString::at() and QString::number().

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Z 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        If you want to show the bytes of a string instead the string you have to convert every char into it's bytes and convert them to printable chars (in your case it looks like you want to display the hex values). See QString::at() and QString::number().

        Z Offline
        Z Offline
        Zunneh
        wrote on last edited by
        #3

        @Christian-Ehrlicher is there another method to directly display the QByteArray into QString instead of display character one by one like you said because i wanna do many encoding value like :
        TIS-620
        TSCII
        UTF-8
        UTF-16
        UTF-16BE
        UTF-16LE
        UTF-32
        UTF-32BE
        UTF-32LE

        my english is average, please use simple words and try to be the most explicit, thank you

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Zunneh said in Displaying a UTF-8 and UTF-16 text in QTextEdit:

          is there another method to directly display the QByteArray into QString instead of display character one by one like you said because i wanna do many encoding

          No, and I don't see why this should be needed. Simply iterate over your QByteArray and convert each char to a (hex)string.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          • Login

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