QGIS for Devs: Unterschied zwischen den Versionen
(fix html h3 attribute) |
(fix syntax) |
||
| Zeile 1: | Zeile 1: | ||
<h2> Setting up a QGIS IDE </ | <h2> Setting up a QGIS workspace</h2> | ||
<h3> IDE </h3> | |||
The [https://code.visualstudio.com/ Visual Studio Code IDE] is a free and lightweight editor with advanced debugging tools, including extensions tailored for Python and QGIS development. It also supports version control and code formatting, making plugin development more efficient. | The [https://code.visualstudio.com/ Visual Studio Code IDE] is a free and lightweight editor with advanced debugging tools, including extensions tailored for Python and QGIS development. It also supports version control and code formatting, making plugin development more efficient. | ||
PyCharm is a Python-focused alternative that is easier to configure than VSCode. | PyCharm is a Python-focused alternative that is easier to configure than VSCode. | ||
The Community Edition is free. It includes a powerful built-in debugger. | The Community Edition is free. It includes a powerful built-in debugger. | ||
<h3> QGIS Extensions </h3> | |||
There is the [https://plugins.qgis.org/plugins/plugin_reloader/ Plugin Reloader] plugin to easily apply your code changes without restarting QGIS. | There is the [https://plugins.qgis.org/plugins/plugin_reloader/ Plugin Reloader] plugin to easily apply your code changes without restarting QGIS. | ||
This greatly speeds up debugging and iteration, making development smoother. | This greatly speeds up debugging and iteration, making development smoother. | ||
<h3> plugin development cycle | <h3> plugin development cycle </h3> | ||
<ol> | <ol> | ||
<li>Modify the Python files of the AIAMAS plugin.</li> | <li>Modify the Python files of the AIAMAS plugin.</li> | ||
| Zeile 25: | Zeile 30: | ||
<strong> On Windows, the full path is typically </strong> | <strong> On Windows, the full path is typically: </strong> | ||
<code>C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins</code></li> | <code>C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins</code></li> | ||
| Zeile 75: | Zeile 80: | ||
</ol> | </ol> | ||
< | <strong>Expected Console Output:</strong> | ||
<pre> | <pre> | ||
| Zeile 83: | Zeile 88: | ||
</pre> | </pre> | ||
< | <strong>Avoiding Crashes with QgsGeometry</strong> | ||
If using: | If using: | ||
| Zeile 105: | Zeile 110: | ||
<h2> Debugging QGIS-Plugins </h2> | <h2> Debugging QGIS-Plugins </h2> | ||
<h3> Debugging Methods </h3> | |||
Developers can debug their QGIS plugin using different methods: | Developers can debug their QGIS plugin using different methods: | ||
| Zeile 117: | Zeile 124: | ||
It displays variables and expressions with their values, formats data structures, and highlights output. Typing is faster, and it can optionally show the filename, line number, and function context. | It displays variables and expressions with their values, formats data structures, and highlights output. Typing is faster, and it can optionally show the filename, line number, and function context. | ||
<h3> Remote Debugging with PyCharm(Debug Server) | <h3> Remote Debugging with PyCharm(Debug Server) </h3> | ||
If running QGIS externally, enable remote debugging: | If running QGIS externally, enable remote debugging: | ||
| Zeile 153: | Zeile 160: | ||
A <code>metadata.txt</code> file is essential as it contains descriptive information about the plugin. This helps users and QGIS manage and understand plugin details effectively. | A <code>metadata.txt</code> file is essential as it contains descriptive information about the plugin. This helps users and QGIS manage and understand plugin details effectively. | ||
<h3> Requirements | <h3> Requirements </h3> | ||
* If <code>hasProcessingProvider</code> exists, it should be mentioned in the "about" section. | * If <code>hasProcessingProvider</code> exists, it should be mentioned in the "about" section. | ||
| Zeile 162: | Zeile 169: | ||
More details about a [https://www.giswiki.ch/QGIS_Plugins_mit_Python metadata.txt]. | More details about a [https://www.giswiki.ch/QGIS_Plugins_mit_Python metadata.txt]. | ||
<h3>Pre-Upload Checklist | <h3>Pre-Upload Checklist </h3> | ||
* Ensure all attributes in <code>metadata.txt</code> are correct. | * Ensure all attributes in <code>metadata.txt</code> are correct. | ||
| Zeile 179: | Zeile 186: | ||
* <strong>ZIP</strong> the plugin directory before upload. | * <strong>ZIP</strong> the plugin directory before upload. | ||
<h3> | <h3>Uploading</h3> | ||
In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab"). | In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab"). | ||
<ol> | <ol> | ||
Version vom 22. Januar 2025, 15:18 Uhr
Setting up a QGIS workspace
IDE
The Visual Studio Code IDE is a free and lightweight editor with advanced debugging tools, including extensions tailored for Python and QGIS development. It also supports version control and code formatting, making plugin development more efficient.
PyCharm is a Python-focused alternative that is easier to configure than VSCode. The Community Edition is free. It includes a powerful built-in debugger.
QGIS Extensions
There is the Plugin Reloader plugin to easily apply your code changes without restarting QGIS. This greatly speeds up debugging and iteration, making development smoother.
plugin development cycle
- Modify the Python files of the AIAMAS plugin.
- Save changes in VSCode or PyCharm.
- Use the Plugin Reloader to apply changes instantly.
Manual installation of a python plugin
If a Python plugin is not available in the official repository, it can be manually installed as follows:
- Clone the repository or create a new plugin in any directory.
- QGIS stores plugins by default in the
python/pluginsfolder within the user-specific QGIS profile directory. On Windows, the full path is typically:C:\Users\Username\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
Replace - The
AppData\Roamingfolder is hidden by default. To access it: * Enable "Show hidden files" in Windows Explorer. * Alternatively, type%appdata%in the Explorer search bar, navigate back one level to AppData.
All the QGIS plugins are stored inside the - Create a shortcut to the cloned or created plugin in this directory, but only include the code folder. Not the entire repository. Files like
README.mdshould not be included in this shortcut.
Username with your actual Windows username.
default/python/plugins folder.
After restarting QGIS, the plugin will now appear under Extensions > Manage and Install Plugins > Installed.
Writing plugins
Documentation for Writing PyQGIS Plugins
Python:
- Writing a QGIS Plugin in Python
- Getting started with a QGIS Plugin
- Structuring Python Plugins
- PyQGIS Developer Cookbook
C++:
Other:
- QGIS Manual - Guide > QGIS Coding and Compilation Guide
- Forum
- Wiki-Artikel
- Community Support
Using the QGIS Python Console
To open the Python console in QGIS:
- Navigate to Menu > Plugins
- Use the shortcut Ctrl+Alt+P
Expected Console Output:
Python Console Use iface to access QGIS API interface or Type help(iface) for more info. _
Avoiding Crashes with QgsGeometry
If using:
geom = iface.activeLayer().selectedFeatures()[0].geometry()
QGIS may crash due to a long-standing unresolved issue 777 with Python bindings.
Instead, use:
feat = iface.activeLayer().selectedFeatures()[0] geom = QgsGeometry(feat.geometry())
Testing:
geom.type(), geom.length(), geom.asPolyline() crashes if the line has three vertices, with only two vertices it returns an empty array.
See also e.g. Python Scripting (PyQGIS).
Debugging QGIS-Plugins
Debugging Methods
Developers can debug their QGIS plugin using different methods:
- print() for basic console output
- qgsMessageLog.logMessage() to log messages inside QGIS
- icecream (ic()) for advanced debugging with variable inspection
We recommend using IceCream (ic()) over print() for debugging. It displays variables and expressions with their values, formats data structures, and highlights output. Typing is faster, and it can optionally show the filename, line number, and function context.
Remote Debugging with PyCharm(Debug Server)
If running QGIS externally, enable remote debugging:
- Install the PyCharm Debugger:
pip install pydevd-pycharm
- Add the following to the plugin code:
import pydevd_pycharm pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True, suspend=False) - In PyCharm, go to Run > Edit Configurations > Python Remote Debug
- Set Host: localhost, Port: 5678, then click Start Debugging
Testing Plugins
- Writing Uni Tests for QGIS Plugins: http://snorf.net/blog/2014/01/04/writing-unit-tests-for-qgis-python-plugins/
- Configuration for Debugging and Unit Testing of PyQGIS: See QGIS Plugins mit Python.
- Testen von Qt-GUI Komponenten: http://www.voom.net/pyqt-qtest-example
Using PyCharm simplifies QGIS plugin development with testing features
Publication of a Plugin
A metadata.txt file is essential as it contains descriptive information about the plugin. This helps users and QGIS manage and understand plugin details effectively.
Requirements
- If
hasProcessingProviderexists, it should be mentioned in the "about" section. - Processing tools should be described in
description,about, and added as atag. - The licence must be GPL 2 or 3. Does the
LICENSEfile exist in the repo? If not, add GPL 3 from the template. - The
homepagemust point to the actual website or at least the repo’sREADME.md(not just the repo itself).
More details about a metadata.txt.
Pre-Upload Checklist
- Ensure all attributes in
metadata.txtare correct.
- Increment the plugin version before uploading.
- Change experimental flag if needed (set to
falseto appear in the default QGIS Plugin list).
- Set an icon (no strict size limit specified).
- Remove unnecessary files (
*.pyc,.gitignore, IDE-specific files, etc.).
- Use only ASCII characters in filenames (Umlauts will cause errors).
- Plugin directory names cannot contain hyphens.
- ZIP the plugin directory before upload.
Uploading
In order to upload a plugin on the official QGIS page a OSGeo ID (user) is required (e.g. user account "geometalab").
- The user account can be created here under sign-in: https://www.osgeo.org/
- On the page login with your OSGeo ID.
- The plugin (ZIP archive) can be uploaded here. Check experimental field if the plugin still is experimental.
- After uploading the plugin it won't be immediately available in the plugin list. It first has to get approved. The approval can take up to 2 weeks. Check How to add your plugin to this repository for the criteria the plugin has to meet in order to get approved.