Search code examples
qtutf-8qstringpersian

Problems in converting to UTF-8 in Qt


I try to show a persian string in Qt:

QMessageBox msg;

QString str = "یا حسین";
msg.setText(QString::fromUtf8(str));
msg.exec();

but it shows the following error :

/home/msi/Desktop/VoMail Project/Project/VoMail-build-desktop-Qt_4_8_1_in_PATH__System__Release/../VoMail/mainwindow.cpp:40: error: no matching function for call to 'QString::fromUtf8(QString&)'

I want to use a string variable, and not a string directly.

How can I convert a QString variable to Utf8?


Solution

  • As seen here, QString::fromUtf8() does not accept an argument of type QString. You must give it a const char *, so you could rewrite it like this:

    QMessageBox msg;
    
    QString str = QString::fromUtf8("یا حسین");
    msg.setText(str);
    msg.exec();