Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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


  • Qt Champions 2019

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



  • @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


  • Qt Champions 2019

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


Log in to reply