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. QString::fromStdString/toStdString wrong behavior.
Forum Updated to NodeBB v4.3 + New Features

QString::fromStdString/toStdString wrong behavior.

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 3.9k Views 1 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.
  • N Offline
    N Offline
    nikitablack
    wrote on last edited by
    #1

    Greetings!

    I have some std::string and trying to create a QString from it. Next I'm trying to get std::string back, but I get different string.

    @const char* c1 = s1.data(); // s1 - std::string
    const char* c2 = QString::fromStdString(s1).toStdString().data();
    int comp = strcmp(c1, c2); // -1 in my case@

    or
    @QString::fromStdString(s1).toStdString() == s1 // false@

    The docs says that QT should be configured with STL compatibility, but as far I know it's done automatically.

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

      @
      const char* c2 = QString::fromStdString(s1).toStdString().data();
      @
      This is a (common) mistake and a bug.
      At the ; the temporary returned by toStdString is destroyed so c2 points to invalid memory region.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nikitablack
        wrote on last edited by
        #3

        Thank you, you're right. But anyway, with lvalues the behavior is the same. Or take a look at the second example.

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

          Works for me. What string do you have and what encoding does it use? Qt assumes utf-8 when using fromStdString. If it's not utf-8 the resulting QString might become garbage.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nikitablack
            wrote on last edited by
            #5

            To be honest, I don't know the encoding. I'm using a string from protobuf's SerializeToString().

            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

              We use SerializeToArray() into a QByteArray, maybe that's an alternative for you.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nikitablack
                wrote on last edited by
                #7

                Yeah. I'm using it right now. Just wondering what's wrong with a string.

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

                  If you don't know the encoding I would plug the debugger and see what the data is in there. Print that stuff out before and after the conversion. See if there are any \0 or non-utf8 characters etc. Basically eyeball the problem ;)

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    nikitablack
                    wrote on last edited by
                    #9

                    Yeah, tried this. Can't understand. Protobuf docs don't say what encoding do they use. In memory editor in debugger I can see that string from QString have some extra bytes.

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

                      The docs of the fromStdString() say that it uses fromUtf8 which expects a null terminated string. This is also what std::string contains. If the extra bytes you see are at the end it would mean that SerializeToString() somehow creates an invalid std::string object.

                      SerializeToArray takes the size parameter explicitly thus probably avoiding the problem.

                      A though came to me. How are you linking to the protobuf? Was it built using the same compiler version and settings as your app? The sizes and layout of std::string varies between compiler and STL versions. Maybe you're dealing with corrupted string objects?

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        nikitablack
                        wrote on last edited by
                        #11

                        No, the compiler is the same. It's hard to reproduce a problem because with some test string everything works. The problem arised only with serialized string. And the extra bytes I found in the beginning and in the end. SerializeToArray is working for me.

                        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