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] Convert std::string to QString in Qt 4.8.1?

[Solved] Convert std::string to QString in Qt 4.8.1?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 9.7k Views
  • 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.
  • C Offline
    C Offline
    CAD_coding
    wrote on 4 Jan 2014, 14:42 last edited by
    #1

    Hi
    I have a std::string myString which needs to be converted to QString?
    How do I do this?
    I am aware of following method:

    @QString myQString = QString::fromAscii(myString.c_str(), myString.length());@

    But the problem is that I have millions of such strings.
    So I would like to know the most efficient method for this translation?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mranger90
      wrote on 4 Jan 2014, 14:51 last edited by
      #2

      Wont QString::fromStdString() work ?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CAD_coding
        wrote on 4 Jan 2014, 14:56 last edited by
        #3

        The documentation says

        bq. This constructor is only available if Qt is configured with STL compatibility enabled.

        What does this mean?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tannin
          wrote on 4 Jan 2014, 15:51 last edited by
          #4

          When compiling Qt from source you can disable support for these functions but to my knowledge the pre-compiled distribution is compiled with the support enabled.

          However: fromStdString always assumes ascii encoding, so using it is a bad idea unless the strings are internal strings or you're sure your code will never come into contact with non-english languages.

          Otherwise your strings might contain utf8 or a local 8 bit encoding (i.e. latin1) so fromAscii or fromStdString will lose information.

          You should work with QString::fromLocal8Bit or QString::fromUtf8, depending on where the c-strings come from.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t3685
            wrote on 4 Jan 2014, 16:24 last edited by
            #5

            [quote author="Tannin" date="1388850668"]When compiling Qt from source you can disable support for these functions but to my knowledge the pre-compiled distribution is compiled with the support enabled.

            However: fromStdString always assumes ascii encoding, so using it is a bad idea unless the strings are internal strings or you're sure your code will never come into contact with non-english languages.

            Otherwise your strings might contain utf8 or a local 8 bit encoding (i.e. latin1) so fromAscii or fromStdString will lose information.

            You should work with QString::fromLocal8Bit or QString::fromUtf8, depending on where the c-strings come from.[/quote]

            I believe this has changed in Qt 5.

            https://qt-project.org/doc/qt-5.0/qtcore/qstring.html#fromStdString

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CAD_coding
              wrote on 5 Jan 2014, 13:38 last edited by
              #6

              @tanin thanks for the information.
              I am working with numbers, so it will always be ASCII.
              @t3685 great improvement I think.

              1 Reply Last reply
              0

              1/6

              4 Jan 2014, 14:42

              • Login

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