refactor: replace pickle files by QSettings

Issue #325 resolved
Reimar Bauer created an issue

We have used pickle files to remember QT states of the applications. This could be changed to the builtin QSettings an example on:

https://evileg.com/en/post/219/

Comments (10)

  1. Shivashis Padhi

    I realize a few things, and this change might do more harm than good in a long run.

    For example, this is a sample script to work with QSettings if we have array with diverse datatypes.

    save data

    settings_const = {
                'a': "asdfasdf",
                'b': True,
                'c': [1,23,4,'asdf']
            }
            for key in settings_const.keys():
                if type(settings_const[key]).__name__ == 'list':
                    q_settings.beginWriteArray(key)
                    for i in range(len(settings_const[key])):
                        q_settings.setArrayIndex(i)
                        q_settings.setValue("value", settings_const[key][i])
                    q_settings.endArray()
                else:
                    q_settings.setValue(key, settings_const[key])
            q_settings.sync()
    

    get data

    # with _fs.open(_name, "rb") as fileobj:
                #     settings = pickle.load(fileobj)
                #     logging.debug(settings)
                child_keys = q_settings.childKeys()
                for key in child_keys:
                    value = q_settings.value(key)
                    if value == "true":
                        settings[key] = True
                    elif value == "false":
                        settings[key] = False
                    else:
                        settings[key] = value
    
    
                all_keys = q_settings.allKeys()
                array_keys = getArrayKey(all_keys)
                key = array_keys[0]
                settings[key] = []
                print(array_keys)
                size = q_settings.beginReadArray(key)
                for i in range(size):
                    print("reached here2")
                    q_settings.setArrayIndex(i)
                    val = q_settings.value("value")
                    try:
                        val = int(val)
                    except:
                        pass
                    settings[key].append(val)
                q_settings.endArray()
                print(settings)
    

    This process would get inefficient as more complex data gets stored in QSettings.

  2. Shivashis Padhi

    I might be wrong, as I am new to both Qt and MSS. But if these doubts are cleared, I have the rest of the code almost ready for a pull request.

  3. Reimar Bauer reporter

    I'm not tried that but I guess we can build hierarchical groups on dictionaries

    e.g.

    https://stackoverflow.com/questions/18870057/having-hierarchical-groups-in-ini-file-for-qsettings

    the data type could be retrieved that way: https://stackoverflow.com/questions/9257422/how-to-get-the-original-python-data-from-qvariant

    interesting discussion too: https://stackoverflow.com/questions/13729189/is-it-possible-to-pickle-a-qsettings-object

    The different types are still an issue.

    How much data will that become?

  4. Shivashis Padhi

    QVariant seems to handle complex dictionaries as well, and list with multiple datatype. Bool seems to be stored as string but that's no biggie.

  5. Log in to comment