PyQt5 QSettings Note

保存应用窗口大小和位置的例子

    def read_settings(self):
        settings = QSettings("Dormouse", "LakeTai")
        pos = settings.value("pos", QPoint(200, 200))
        size = settings.value("size", QSize(400, 400))
        self.resize(size)
        self.move(pos)

    def write_settings(self):
        settings = QSettings("Dormouse", "LakeTai")
        settings.setValue("pos", self.pos())
        settings.setValue("size", self.size())

In [16]:
from PyQt5.QtCore import QSettings

最简单的用法


In [17]:
myOrganization = 'MyOrg'  # 组织名称
myApplication = 'MyApp'  # 应用名称
settings = QSettings(myOrganization, myApplication)
settings.setValue("editor/wrapMargin", 68)
print (settings.value("editor/wrapMargin"))
print (settings.value("editor/someth"))


68
None

In [29]:
# 如果在程序中多处用到 QSettings ,那么可以一次性定义,然后统一使用
from PyQt5.QtCore import QCoreApplication
QCoreApplication.setOrganizationName("MyOrg2nd")
QCoreApplication.setOrganizationDomain("MyOrg2nd.com")
QCoreApplication.setApplicationName("MyApp2nd")
settings_2nd = QSettings()
print (settings.applicationName())
print (settings_2nd.applicationName())
print (settings_2nd.organizationName())


MyApp
MyApp2nd
MyOrg2nd.com

In [18]:
# 测试键是否存在
settings.contains("editor")
settings.contains("editor/wrapMargin")


Out[18]:
True

In [19]:
settings.remove("editor") #删除一个键,键不存在也不会报错
settings.contains("editor/wrapMargin")


Out[19]:
False

In [20]:
settings.clear()  #清空所有键(只是清空自己添加的,系统固有的还在)
settings.allKeys()  #所有键


Out[20]:
['AKDeviceUnlockState',
 'AppleActionOnDoubleClick',
 'AppleAntiAliasingThreshold',
 'AppleEnableMouseSwipeNavigateWithScrolls',
 'AppleLanguages',
 'AppleLocale',
 'AppleMeasurementUnits',
 'AppleMetricUnits',
 'AppleMiniaturizeOnDoubleClick',
 'ApplePressAndHoldEnabled',
 'AppleTextDirection',
 'Country',
 'MultipleSessionEnabled',
 'NSAutomaticDashSubstitutionEnabled',
 'NSAutomaticQuoteSubstitutionEnabled',
 'NSAutomaticSpellingCorrectionEnabled',
 'NSNavPanelFileLastListModeForOpenModeKey',
 'NSNavPanelFileListModeForOpenMode2',
 'NSNavPanelSidebarKeyForOpen',
 'NSNavRecentPlaces',
 'NSPersonNameDefaultDisplayNameOrder',
 'NSPersonNameDefaultShortNameFormat',
 'NSPersonNameDefaultShouldPreferNicknamesPreference',
 'NSPreferredWebServices',
 'NSUserDictionaryReplacementItems',
 'NavPanelFileListModeForOpenMode',
 'WebAutomaticSpellingCorrectionEnabled',
 'com/apple/AppleModemSettingTool/LastCountryCode',
 'com/apple/ColorSync/Devices',
 'com/apple/TimeZonePref/Last_Selected_City',
 'com/apple/mouse/scaling',
 'com/apple/mouse/tapBehavior',
 'com/apple/preferences/timezone/selected_city',
 'com/apple/springing/delay',
 'com/apple/springing/enabled',
 'com/apple/trackpad/enableSecondaryClick',
 'com/apple/trackpad/fiveFingerPinchSwipeGesture',
 'com/apple/trackpad/forceClick',
 'com/apple/trackpad/fourFingerHorizSwipeGesture',
 'com/apple/trackpad/fourFingerPinchSwipeGesture',
 'com/apple/trackpad/fourFingerVertSwipeGesture',
 'com/apple/trackpad/momentumScroll',
 'com/apple/trackpad/pinchGesture',
 'com/apple/trackpad/rotateGesture',
 'com/apple/trackpad/scaling',
 'com/apple/trackpad/scrollBehavior',
 'com/apple/trackpad/threeFingerDragGesture',
 'com/apple/trackpad/threeFingerHorizSwipeGesture',
 'com/apple/trackpad/threeFingerTapGesture',
 'com/apple/trackpad/threeFingerVertSwipeGesture',
 'com/apple/trackpad/twoFingerDoubleTapGesture',
 'com/apple/trackpad/twoFingerFromRightEdgeSwipeGesture',
 'com/apple/trackpad/version',
 'com/apple/updatesettings_did_disable_ftp']

In [21]:
settings.fileName() #储存位置


Out[21]:
'/Users/dormouse/Library/Preferences/com.myorg.MyApp.plist'

In [24]:
settings_ini = QSettings("conf.ini", QSettings.IniFormat) #使用 ini 文件来储存
settings_ini.fileName()


Out[24]:
'/Users/dormouse/project/laketai/conf.ini'