PySide:QSettings作为类变量具有不一致的行为

时间:2015-02-19 22:58:56

标签: pyside qsettings

我们为Maya 2015编写了许多PySide脚本,并使用QSettings保存设置。通常我们在“readSettings”和“writeSettings”函数中创建QSettings对象。今天我尝试将QSettings对象作为类变量。但这引起了一些奇怪的影响。通常以<type 'unicode'>形式返回的某些值开始以<type 'bool'>的形式返回,但不是所有的时间!

这是我写的一个测试脚本来说明问题:

import shiboken

from PySide import QtGui, QtCore
from maya import OpenMayaUI

#------------------------------------------------------------------------------
def getMayaMainWindow():
    parentWindow = OpenMayaUI.MQtUtil.mainWindow()
    if parentWindow:
        return shiboken.wrapInstance(long(parentWindow), QtGui.QWidget)

#------------------------------------------------------------------------------
class TestQSettingsWin(QtGui.QMainWindow):
    def __init__(self, parent=getMayaMainWindow()):
        super(TestQSettingsWin, self).__init__(parent)

        self.setWindowTitle('Test QSettings')
        self.setObjectName('testAllMMessagesWindow')

        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainLayout = QtGui.QVBoxLayout(self.centralWidget)
        self.checkBox = QtGui.QCheckBox('check box')
        self.mainLayout.addWidget(self.checkBox)

        self.readSettings()

    def closeEvent(self, event):
        self.writeSettings()

    def getQSettingsLocation(self):
        raise NotImplementedError('Subclasses of TestQSettingsWin need to '
                                  'implement "getQSettingsLocation"".')

    def readSettings(self):
        setting = self.getQSettingsLocation()
        self.restoreGeometry(setting.value('geometry'))
        self.restoreState(setting.value('windowState'))
        print type(setting.value('checkBox'))

    def writeSettings(self):
        setting = self.getQSettingsLocation()
        setting.setValue('geometry', self.saveGeometry())
        setting.setValue('windowState', self.saveState())
        setting.setValue('checkBox', self.checkBox.isChecked())

#------------------------------------------------------------------------------
class TestQSettingsClassVar(TestQSettingsWin):
    savedSettings = QtCore.QSettings(QtCore.QSettings.IniFormat,
                                     QtCore.QSettings.UserScope,
                                     "Test",
                                     "TestQSettings1")
    def getQSettingsLocation(self):
        return self.savedSettings

#------------------------------------------------------------------------------
class TestQSettingsDefScope(TestQSettingsWin):
    def getQSettingsLocation(self):
        setting = QtCore.QSettings(QtCore.QSettings.IniFormat,
                                   QtCore.QSettings.UserScope,
                                   "Test",
                                   "TestQSettings3")
        return setting

#------------------------------------------------------------------------------
def showTestWindows():
    test1 = TestQSettingsClassVar()
    test1.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
    test1.show()
    test2 = TestQSettingsDefScope()
    test2.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
    test2.show()

以下是在交互式会话中运行它的结果:

>>> import testQSettings
>>> testQSettings.showTestWindows()
<type 'NoneType'>
<type 'NoneType'>
>>> testQSettings.showTestWindows()
<type 'bool'>
<type 'unicode'>
>>> testQSettings.showTestWindows()
<type 'bool'>
<type 'unicode'>
>>> reload(testQSettings)
# Result: <module 'testQSettings' from 'C:/Users/becca/Documents/maya/2015-x64/scripts\testQSettings.pyc'> # 
>>> testQSettings.showTestWindows()
<type 'unicode'>
<type 'unicode'>
>>> testQSettings.showTestWindows()
<type 'bool'>
<type 'unicode'>
>>> testQSettings.showTestWindows()
<type 'bool'>
<type 'unicode'>

如您所见,在需要时始终创建QSettings对象会返回数据值的<type 'unicode'>结果。但是,当重新加载模块时,将QSettings对象创建为类变量将返回<type 'bool'>结果除外),然后返回<type 'unicode'>

任何人都可以解释这种奇怪的行为吗?是否有规则我不应该将QSettings对象作为类变量?

1 个答案:

答案 0 :(得分:1)

设置对象必须在将各种不同类型的值写入磁盘之前将其序列化。这通常在删除设置对象时完成(或者,如果正在运行事件循环,则可以定期将未保存的数据刷新到磁盘)。

每次调用settings.value()时,未保存的数据都没有刷新到磁盘,它将返回原始类型的反序列化值。

可以通过调用settings.sync()来强行刷新数据,但我强烈建议不要尝试这样做。无论何时想要读取或写入值,都应该始终创建一个新的QSettings对象,并确保在使用它之后将其删除。这应该足以保证一致性。