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. Using QSettings with data stored in a QByteArray, or QIODevice
Forum Updated to NodeBB v4.3 + New Features

Using QSettings with data stored in a QByteArray, or QIODevice

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.2k Views 2 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.
  • Guy GizmoG Offline
    Guy GizmoG Offline
    Guy Gizmo
    wrote on last edited by Guy Gizmo
    #1

    I have data in ini format stored in a QByteArray, and I'd like to be able to read and write to it using QSettings. Unfortunately, out of the box QSettings only seems capable of working with files specifically, and can't be used with a QByteArray or an instance of a class that derives from QIODevice such as QBuffer.

    So far, I've found two ways one way of dealing with this:

    1. Dump the data temporarily into a file, probably using QTemporaryFile, and read that using QSettings
    2. Create a custom implementation of QAbstractFileEngine that can read from the data in the byte array as though it were a file, as suggested in this ten year old thread. (edit: I've just discovered that QAbstractFileEngine is no longer public in Qt 5, so it's not a viable solution for me.)

    Neither of these options are very appealing. Ten years has gone by since the suggestion of using QAbstractFileEngine. Is there a better method available now?

    If this is currently not possible, consider this a feature suggestion that QSettings be able to work with anything derived from QIODevice that has the necessary capabilities.

    B 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use a QTemporaryFile and provide a patch to allow a QIODevice.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Guy GizmoG fcarneyF 2 Replies Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Use a QTemporaryFile and provide a patch to allow a QIODevice.

        Guy GizmoG Offline
        Guy GizmoG Offline
        Guy Gizmo
        wrote on last edited by Guy Gizmo
        #3

        Writing a patch would be a massive undertaking and one I most definitely don't have time for right now. 😝

        I'm still curious if there's a method available other than using QTemporaryFile.

        1 Reply Last reply
        0
        • Guy GizmoG Guy Gizmo

          I have data in ini format stored in a QByteArray, and I'd like to be able to read and write to it using QSettings. Unfortunately, out of the box QSettings only seems capable of working with files specifically, and can't be used with a QByteArray or an instance of a class that derives from QIODevice such as QBuffer.

          So far, I've found two ways one way of dealing with this:

          1. Dump the data temporarily into a file, probably using QTemporaryFile, and read that using QSettings
          2. Create a custom implementation of QAbstractFileEngine that can read from the data in the byte array as though it were a file, as suggested in this ten year old thread. (edit: I've just discovered that QAbstractFileEngine is no longer public in Qt 5, so it's not a viable solution for me.)

          Neither of these options are very appealing. Ten years has gone by since the suggestion of using QAbstractFileEngine. Is there a better method available now?

          If this is currently not possible, consider this a feature suggestion that QSettings be able to work with anything derived from QIODevice that has the necessary capabilities.

          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @Guy-Gizmo said in Using QSettings with data stored in a QByteArray, or QIODevice:

          consider this a feature suggestion

          This is an user forum.
          If you want to give suggestion to dev team you should file an issue to https://bugreports.qt.io

          Guy GizmoG 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            Use a QTemporaryFile and provide a patch to allow a QIODevice.

            fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in Using QSettings with data stored in a QByteArray, or QIODevice:

            provide a patch

            Are you talking about making a format and registering it?

            I was thinking something from boost would be helpful. Not sure how the format interface works though.

            C++ is a perfectly valid school of magic.

            Guy GizmoG 1 Reply Last reply
            0
            • B Bonnie

              @Guy-Gizmo said in Using QSettings with data stored in a QByteArray, or QIODevice:

              consider this a feature suggestion

              This is an user forum.
              If you want to give suggestion to dev team you should file an issue to https://bugreports.qt.io

              Guy GizmoG Offline
              Guy GizmoG Offline
              Guy Gizmo
              wrote on last edited by
              #6

              @Bonnie I just checked my account on bugreports.qt.io and found I made just such a request back in February 2016. Hasn't gotten any votes since then. I doubt this feature will be added, sadly!

              1 Reply Last reply
              0
              • fcarneyF fcarney

                @Christian-Ehrlicher said in Using QSettings with data stored in a QByteArray, or QIODevice:

                provide a patch

                Are you talking about making a format and registering it?

                I was thinking something from boost would be helpful. Not sure how the format interface works though.

                Guy GizmoG Offline
                Guy GizmoG Offline
                Guy Gizmo
                wrote on last edited by
                #7

                @fcarney said in Using QSettings with data stored in a QByteArray, or QIODevice:

                I was thinking something from boost would be helpful. Not sure how the format interface works though.

                I considered adding another format that uses a separate ini parser that can handle data in memory, but that only gets me halfway there. I need to be able to read ini formatted files already written using QSettings, which means that certain types (for example QByteArray, QStringList, etc.) have been encoded using a method specific to QSettings' ini formatter. As far as I know QSettings doesn't expose its facility for encoding and decoding those values.

                1 Reply Last reply
                0
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #8

                  @fcarney said in Using QSettings with data stored in a QByteArray, or QIODevice:

                  something from boost

                  That is why I linked a cross platform INI file reader/writer from boost "something from boost".

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  0
                  • Guy GizmoG Offline
                    Guy GizmoG Offline
                    Guy Gizmo
                    wrote on last edited by
                    #9

                    Just a quick follow-up: I ended up using QAbstractFileEngine to solve this anyway, even though it's private in Qt 5.15. If it ever is removed or otherwise stops becoming a viable method, I'll update my code accordingly.

                    That said, if anyone can suggest another viable method that doesn't rely on writing the data out to a temporary file, I'd love to hear it!

                    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