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. QSettings::setValue() replacing custom type with invalid type?

QSettings::setValue() replacing custom type with invalid type?

Scheduled Pinned Locked Moved Solved General and Desktop
qsettingsqvariantcustom data
6 Posts 3 Posters 666 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
    CJha
    wrote on 28 Nov 2023, 09:53 last edited by
    #1

    Hi, in my application I am storing the settings using settings.ini file format. I have a custom data type that I register and then retrieve and store in the settings.ini file. During the startup, before the custom data type is registered using qRegisterMetaType, I call QSettings::setValue() to set a normal integer value (completely independent from the custom data type) in the settings.ini file. But doing this makes the QSettings replace all the custom types currently in the settings.ini file with an invalid QVariant type while debugging the output QVariant::load: unknown user type with name CustomDataType, is this behavior normal?

    J J 2 Replies Last reply 28 Nov 2023, 10:07
    0
    • C CJha
      28 Nov 2023, 10:15

      @JonB Thanks! I was thinking the same, but it is kind of a pity that a single key-value pair cannot be accessed without checking each entry in the .ini file, after all keys are stored as text and should be easy to filter out.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Nov 2023, 10:18 last edited by
      #6

      @CJha The problem is that you can't just replace something in a file if new value has different lenght than the old one. In this case you need to read the file into memory, replace the value and write it back. To read such an ini file and replace something there Qt needs to know how to interpret the data it reads.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • C CJha
        28 Nov 2023, 09:53

        Hi, in my application I am storing the settings using settings.ini file format. I have a custom data type that I register and then retrieve and store in the settings.ini file. During the startup, before the custom data type is registered using qRegisterMetaType, I call QSettings::setValue() to set a normal integer value (completely independent from the custom data type) in the settings.ini file. But doing this makes the QSettings replace all the custom types currently in the settings.ini file with an invalid QVariant type while debugging the output QVariant::load: unknown user type with name CustomDataType, is this behavior normal?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 28 Nov 2023, 10:07 last edited by
        #2

        @CJha said in QSettings::setValue() replacing custom type with invalid type?:

        is this behavior normal?

        I think so, because at that time you did not yet registered you custom type, right?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        C 1 Reply Last reply 28 Nov 2023, 10:12
        1
        • C CJha
          28 Nov 2023, 09:53

          Hi, in my application I am storing the settings using settings.ini file format. I have a custom data type that I register and then retrieve and store in the settings.ini file. During the startup, before the custom data type is registered using qRegisterMetaType, I call QSettings::setValue() to set a normal integer value (completely independent from the custom data type) in the settings.ini file. But doing this makes the QSettings replace all the custom types currently in the settings.ini file with an invalid QVariant type while debugging the output QVariant::load: unknown user type with name CustomDataType, is this behavior normal?

          J Offline
          J Offline
          JonB
          wrote on 28 Nov 2023, 10:08 last edited by JonB
          #3

          @CJha
          Thinking aloud. In order to do any setValue() (or just value(), or any other QSettings operation) the Qt code is going to have to read the whole of the .ini file and parse it correctly. It cannot just "access a single key-value". So it seems reasonable to me that it will fall over if the file contains any unknown custom data type at that point. You really need to call any qRegisterMetaType() before you try to access anything in the .ini file, ISTM.

          C 1 Reply Last reply 28 Nov 2023, 10:15
          2
          • J jsulm
            28 Nov 2023, 10:07

            @CJha said in QSettings::setValue() replacing custom type with invalid type?:

            is this behavior normal?

            I think so, because at that time you did not yet registered you custom type, right?

            C Offline
            C Offline
            CJha
            wrote on 28 Nov 2023, 10:12 last edited by
            #4

            @jsulm Yes, the custom data type is registered afterward when the code execution reaches that point.

            1 Reply Last reply
            0
            • J JonB
              28 Nov 2023, 10:08

              @CJha
              Thinking aloud. In order to do any setValue() (or just value(), or any other QSettings operation) the Qt code is going to have to read the whole of the .ini file and parse it correctly. It cannot just "access a single key-value". So it seems reasonable to me that it will fall over if the file contains any unknown custom data type at that point. You really need to call any qRegisterMetaType() before you try to access anything in the .ini file, ISTM.

              C Offline
              C Offline
              CJha
              wrote on 28 Nov 2023, 10:15 last edited by
              #5

              @JonB Thanks! I was thinking the same, but it is kind of a pity that a single key-value pair cannot be accessed without checking each entry in the .ini file, after all keys are stored as text and should be easy to filter out.

              J 1 Reply Last reply 28 Nov 2023, 10:18
              0
              • C CJha
                28 Nov 2023, 10:15

                @JonB Thanks! I was thinking the same, but it is kind of a pity that a single key-value pair cannot be accessed without checking each entry in the .ini file, after all keys are stored as text and should be easy to filter out.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 28 Nov 2023, 10:18 last edited by
                #6

                @CJha The problem is that you can't just replace something in a file if new value has different lenght than the old one. In this case you need to read the file into memory, replace the value and write it back. To read such an ini file and replace something there Qt needs to know how to interpret the data it reads.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • C CJha has marked this topic as solved on 28 Nov 2023, 10:44

                2/6

                28 Nov 2023, 10:07

                4 unread
                • Login

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