1

我们为 Maya 2015 编写了许多 PySide 脚本,并使用QSettings. 通常我们QSettings在“readSettings”和“writeSettings”函数中创建对象。今天我尝试使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对象设为类变量?

4

1 回答 1

1

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

在未保存的数据未刷新到磁盘的所有时间内,无论何时调用settings.value(),它都会以其原始类型返回未序列化的值。

可以通过调用自己强制刷新数据settings.sync(),但我强烈建议不要尝试此操作。每当您想读取或写入值时,您应该始终创建一个新QSettings对象,并确保在您使用它后将其删除。这应该足以保证一致性。

于 2015-02-20T00:15:13.040 回答