我们为 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
对象设为类变量?