QGIS-2.8-PyQGISDeveloperCookbook-en(6)
时间:2025-07-09
时间:2025-07-09
为开发人员准备的文档,涵盖QGIS的多种功能实现和代码。
1.1.2Thestartup.py le
EverytimeQGISstarts,theuser’sPythonhomedirectory(usually:.qgis2/python)issearchedfora le
namedstartup.py,ifthat leexists,itisexecutedbytheembeddedPythoninterpreter.
1.2PythonConsole
Forscripting,itispossibletotakeadvantageofintegratedPythonconsole.Itcanbeopenedfrommenu:Plugins
→PythonConsole.Theconsoleopensasanon-modalutility
window:
Figure1.1:QGISPythonconsole
Thescreenshotaboveillustrateshowtogetthelayercurrentlyselectedinthelayerlist,showitsIDandoptionally,
ifitisavectorlayer,showthefeaturecount.ForinteractionwithQGISenvironment,thereisaifacevariable,
whichisaninstanceofQgsInterface.Thisinterfaceallowsaccesstothemapcanvas,menus,toolbarsand
otherpartsoftheQGISapplication.
Forconvenienceoftheuser,thefollowingstatementsareexecutedwhentheconsoleisstarted(infutureitwillbe
possibletosetfurtherinitialcommands)
Forthosewhichusetheconsoleoften,itmaybeusefultosetashortcutfortriggeringtheconsole(withinmenu
Settings→Con gureshortcuts...)
1.3PythonPlugins
QGISallowsenhancementofitsfunctionalityusingplugins.ThiswasoriginallypossibleonlywithC++language.
WiththeadditionofPythonsupporttoQGIS,itisalsopossibletousepluginswritteninPython.Themain
advantageoverC++pluginsisitssimplicityofdistribution(nocompilingforeachplatformneeded)andeasier
development.
ManypluginscoveringvariousfunctionalityhavebeenwrittensincetheintroductionofPythonsupport.Theplu-
gininstallerallowsuserstoeasilyfetch,upgradeandremovePythonplugins.SeethePythonPluginRepositories
pageforvarioussourcesofplugins.
CreatingpluginsinPythonissimple,seeDevelopingPythonPluginsfordetailedinstructions.