Qt World Summit: Register Today!

[SOLVED] Non-latin characters encoding

  • Hi everyone!
    I have Qt 5.3, WIn8.1 and source code in ANSI1251. So I have

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    It works fine, if I open files with latin names or use the path, which contains only latin characters. But if I open file with non-latin characters in the name or in the path, I see something like that:


    Any idea, how can I fix it?
    I just need the correct path and the file name in QString.

    Thanks for help.

  • Lifetime Qt Champion

    Are you sure they are messed up ?
    QDebug might not display it correctly.


    QStringList files = QFileDialog::getOpenFileNames(/* ... */);
    QMessageBox::information(this, tr("Test"), files.takeFirst());

    as they talk about here

  • Hi!
    Thank you for your help. So the path looks correct in QMessageBox, but how can I pass this path into the function

    void processFileScript(std::string filePath)


    Now I have smth like that and it doesn't work:

    QString file = QFileDialog::getOpenFileNames(/* ... */);
    QMessageBox::information(this, tr("Test"), file);

    So I still can't open this file.

  • Lifetime Qt Champion

    @Klayman said:
    Have you tried to pass it as QString to processFileScript ?
    and use QString in place of std::string.
    Or use QString:: toStdWString()
    and std::wstring.
    I think you need full unicode for it to work and std::string works best with UTF-8 so
    when you call toStdString, you break it. (I guess)

    If you program processFileScript also, I would use QString and QFile and it should just work.

  • Moderators

    std::string is just storage (array of bytes). You need to know what encoding your processFileScript expects in that storage. The string returned by QString::toStdString is encoded using UTF-8. If your function expects something else you'll need another means of conversion. What encoding does the function expect?


    and it doesn't work

    This is the least useful description of a problem on a programming forum you can give ;) Please try to be more specific ;)

  • Hi, mrjj !
    Thanks for your help!
    I wrote processFileScript with QString instead of std::string and it works fine.

  • Hi Chris!
    I think it's CP1251. But anyway, it started to work when I change std:string to QString.
    And thanks for your remark, I absolutely agree with you, it was useless expression =)

Log in to reply