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. Conversion question

Conversion question

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.8k 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.
  • A Offline
    A Offline
    Archie888
    wrote on last edited by Archie888
    #1

    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
    0
    • L Offline
      L Offline
      Leonardo
      wrote on last edited by
      #2

      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
      1
      • A Offline
        A Offline
        Archie888
        wrote on last edited by Archie888
        #3

        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?

        kshegunovK L 2 Replies Last reply
        0
        • A 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?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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
          1
          • A 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?

            L Offline
            L Offline
            Leonardo
            wrote on last edited by Leonardo
            #5

            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
            1
            • A Offline
              A Offline
              Archie888
              wrote on last edited by Archie888
              #6

              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
              0
              • A Offline
                A Offline
                Archie888
                wrote on last edited by
                #7

                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
                0

                • Login

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