Qt String Encoding Problem

Asked

Viewed 151 times

4

I’m having a problem encoding strings on Qt. By showing something in a QMessageBox or give a drawText in a paintEvent, my strings come out badly formatted, as if they were not in the correct encoding.

This is how it appears to me:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Code of the Linechart:

if(bTitle){
    p.setPen(titleColor);
    p.drawText((width()/2 - (getTitle().size() * 5)),40, getTitle());
}

and getTitle is:

QString LineChart::getTitle(){
    return this->Title;
}

Use Debian 7.1 64 bits.

Does anyone know how to solve?

  • Hello Avelino, could you post relevant parts of your code? Also show the output of file -i [nome do arquivo de código fonte] for relevant files. The shortest way to success is to write and read everything with UTF-8 (see that article in English).

  • 1

    Still on that front, for fixed strings it is always good to fix the encoding: QString str = QString::fromUtf8("Instruções");

  • Hello, This is the output for my files: avelino@amn:~/Projects/Qt/Flappybird$ file -i Animation.cpp Animation.cpp: text/x-c; charset=utf-8

  • I tried using: Qstring str = Qstring::fromUtf8("text"); Qmessagebox::information(this, tr(str)); but it doesn’t let because tr has to receive a char array or pointer.

  • 1

    I used a trUtf8() in Qmessagebox and solved the problem. But paintEvent still persists. I wonder what can be?

  • 1

    Again, this->Title is being set up with fromUtf8 or trUtf8?

  • 1

    I was doing: void Linechart::setTitle(Qstring Title){ this->Title = Title; } Now I did so, and it worked: void Linechart::setTitle(const char * Title){ this->Title = Qstring:::fromUtf8(Title); }

  • Man, very grateful for your help. Documentation has served. =)

Show 3 more comments

1 answer

4


Change your setTitle of:

void LineChart::setTitle(QString Title){
    this->Title = Title;
}

for:

void LineChart::setTitle(const char * Title){
    this->Title = QString::fromUtf8(Title);
}

and add a trUtf8 to Qmessagebox this way:

QMessageBox::information(this, trUtf8("Joaçaba"));

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • 2

    To make it clearer. Avelino is using a version of Qt earlier than 5 (from version 5 onwards encoding UTF-8 is assumed by default and the function trUtf8 became obsolete). I recommend everyone starting with Qt to read the article Strings and encodings in Qt.

  • Use version 4.8.2. = D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.