Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Conversion question

    General and Desktop
    3
    7
    1347
    Loading More Posts
    • 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.
    • A
      Archie888 last edited by Archie888

      Hi,

      I have a large (megabytes) string in a QJsonValue, that I need to convert to QByteArray, as I am sending the string as data with a QNetworkRequest.

      Currently I am doing this:

      myQJsonObject["myQJsonValue"].toString().toUtf8()
      

      ^^Would this incur copying the same data to memory many times for some reason? If so, how would you go about implementing this without unnecessary copyings?

      Cheers,
      -A888

      1 Reply Last reply Reply Quote 0
      • L
        Leonardo last edited by

        Qt stores QString as a 16-bit unicode string. When you call toUtf8(), you're not only converting it to a QByteArray, but also converting its encoding. So data must be copied and you can't avoid the increase in memory usage.

        1 Reply Last reply Reply Quote 1
        • A
          Archie888 last edited by Archie888

          Thanks, great to know. Do you think that with the particular expression here, data would be copied once or twice? Just curious. I would presume and hope that it can be converted to QString from QJsonValue with no cost, is this true?

          Also it is curious that you say that the data needs to be re-encoded. I have been in the understanding that utf-8 is somewhat the standard internal encoding in Qt and QML, is this not true?

          kshegunov L 2 Replies Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @Archie888 last edited by

            @Archie888

            I have been in the understanding that utf-8 is somewhat the standard internal encoding in Qt and QML, is this not true?

            Qt uses UTF-16 internally.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply Reply Quote 1
            • L
              Leonardo @Archie888 last edited by Leonardo

              Source code files are utf8 encoded, but QString itself uses 16bit unicode.

              http://doc.qt.io/qt-5/qstring.html#details

              QJsonValue holds a reference to the string data, so no copy needs to be done by toString().

              1 Reply Last reply Reply Quote 1
              • A
                Archie888 last edited by Archie888

                Also for anyone interested, I asked the same question at StackOverflow, and it was suggested that I use QJsonDocument for such operations, which is probably good advice:

                http://stackoverflow.com/questions/35515065/qt-c-conversion-from-qjsonvalue-to-qbytearray

                I simply needed the QByteArray, I had no idea that toUtf8() had this overhead. QJsonDocument seems to be able to convert straight to QByteArray with QByteArray QJsonDocument::toBinaryData() const

                1 Reply Last reply Reply Quote 0
                • A
                  Archie888 last edited by

                  I do have one extra question: Do you know if it would incur any extra cost to use the QJsonDocument constructor, giving it a large QJsonObject as parameter?

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post