官术网_书友最值得收藏!

Storing and reading global preferences

PyQGIS allows you to store application-level preferences and retrieve them.

Getting ready

This code can be run in any type of PyQGIS application. In this example, we'll run it in the QGIS Python console for an easy demonstration. In this example, we'll change the default CRS for new projects and then read the value back from the global settings.

How to do it…

In this recipe, we will set the default projection used by QGIS for new projects using the Python console:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. We will need to import the Qt core library, as follows:
    from PyQt4.QtCore import *
  4. In the Python console, run the following code:
    settings = QSettings(QSettings.NativeFormat, QSettings.UserScope, 'QuantumGIS', 'QGis')
    settings.setValue('/Projections/projectDefaultCrs', 'EPSG:2278')
    settings.value('/Projections/projectDefaultCrs')

How it works…

This API is actually the Qt API that QGIS relies on for settings. In the QSettings object, we specify the NativeFormat for storage, which is the default format for the platform. On Windows, the format is the registry; on OS X, it's the plist files; and on Unix, it's the text files. The other QSettings parameters are the organization and the application, often used as a hierarchy to store information. Note that even after changing these settings, it may be that none of the properties in the QGIS GUI change immediately. In some cases, such as Windows, the system must be restarted for registry changes to take effect. However, everything will work programmatically.

There's more…

If you want to see all the options that you can change, call the allKeys() method of QSettings; this will return a list of all the setting names.

主站蜘蛛池模板: 梅河口市| 贡嘎县| 兴和县| 汾西县| 上虞市| 和顺县| 定安县| 韩城市| 宾川县| 盘锦市| 荥经县| 同仁县| 兴山县| 开平市| 田林县| 凌源市| 东至县| 北流市| 潼南县| 翁源县| 嘉荫县| 金华市| 宜兰县| 阿拉善左旗| 章丘市| 洛隆县| 巢湖市| 绿春县| 蒙阴县| 永宁县| 永登县| 阆中市| 葫芦岛市| 安新县| 沙坪坝区| 崇仁县| 宝坻区| 英吉沙县| 区。| 镇原县| 故城县|