Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1010

Data type in QSettings

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P2: Important P2: Important
    • 5.12.5, 5.13.1
    • 5.12.2
    • PySide
    • None
    • All
    • 78dad8180d797a647645b74255bfc29c46d7264a (pyside/pyside-setup/5.12)

      The following code stores a list with QSettings:

      from PySide2 import QtCore
      
      settings = QtCore.QSettings('Foo', 'Bar')
      if settings.value('ids') is None:
          item_list = ['a', 'b', 'c']
          settings.setValue('ids', item_list)
      else:
          r = settings.value('ids')
          print(r, type(r))
      

      The previous code must be executed twice, in the first execution it saves the list, and in the second execution the list is obtained correctly.

      ['a', 'b', 'c'] <class 'list'>
      

      But if you pass a list of a single element of type string in the second invocation you get as a string instead of the list:

      from PySide2 import QtCore
      
      settings = QtCore.QSettings('Foo2', 'Bar2')
      if settings.value('ids') is None:
          item_list = ['a']
          settings.setValue('ids', item_list)
      else:
          r = settings.value('ids')
          print(r, type(r))
      

      Output:

      a <class 'str'>
      

      In PyQt5, the QSettings::value() method has an argument that can be used to indicate the type of data that is expected to avoid this type of problem.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            crmaurei Cristian Maureira-Fredes
            eyllanesc Edwin Christian Yllanes Cucho
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes