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. [SOLVED] Non-latin characters encoding
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Non-latin characters encoding

Scheduled Pinned Locked Moved General and Desktop
russian charact
7 Posts 3 Posters 3.0k Views 3 Watching
  • 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.
  • K Offline
    K Offline
    Klayman
    wrote on last edited by Klayman
    #1

    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:

    C:/HEC_GUI/СЃРєСЂРёРїС‚/RI5.ort.homog.txt
    

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

    Thanks for help.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Try

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

      as they talk about here
      https://forum.qt.io/topic/22263/open-unicode-file-name-in-qt4-7/2

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Klayman
        wrote on last edited by
        #3

        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);
        processFileScript(file.toStdString());
        

        So I still can't open this file.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Klayman said:
          Hi
          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.

          K 1 Reply Last reply
          1
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            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?

            Btw.

            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 ;)

            K 1 Reply Last reply
            0
            • mrjjM mrjj

              @Klayman said:
              Hi
              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.

              K Offline
              K Offline
              Klayman
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                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?

                Btw.

                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 ;)

                K Offline
                K Offline
                Klayman
                wrote on last edited by
                #7

                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 =)

                1 Reply Last reply
                0

                • Login

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