Pyqgis Development--Create the first plugin

Source: Internet
Author: User
Tags qt designer

The previous section describes how to configure the Pyqgis development environment and use the Eclipse+pydev tool for Qgis development debugging.
The front should say has already said, later does not have the nonsense, directly enters the topic. Summary

This article will elaborate on how to create a Qgis plug-in using Python. Creating Plug-ins

The simplest way to build a plug-in development project is to use the plugin builder this plugin.

First, open the Qgis software, select "Plugins-> Manage and Install Plugins" in the menu bar, and search for and add the "Plugin Builder" plugin.

After the installation is successful, open the dialog box for the "Plugin Builder" plugin and fill in the corresponding project information as shown below.

Here, I will name the plugin "Myplugin", of course, you can also take any name, just fill in the line. Then click the "Next" button.

The next pop-up is the "about" window, where you can fill in some information. Then continue "Next".

After that, in the window that appears, you need to select the type of plug-in and fill in the name displayed by the plugin in the Qgis software menu bar. Type I chose the default "Tool button with Dialog", which is the window with the button. Name I still fill "myplugin" here. Continue clicking on "Next".

The next pop-up window indicates that the plug-in project needs to be generated by the template, keep the default on it, and continue "Next".

When you get here, you fill out "Bug tracker" and "Repository" information that is directly associated with the Qgis plug-in management platform. It is also said that after your plugin is released, it will be seen by anyone using Qgis's "Mange and Install Plugins" tool, and can be downloaded for use. "Bug tracker" and "Repository" can be filled in with the following GitHub addresses. Bug tracker:https://github.com/geospatialpython/learn/issues repository:https://github.com/geospatialpython/learn/

Of course, our plugin here is just a demonstration, so, put "Flag the plugin as experimental" tick.

After clicking "Next" again, the Plugin Save Location dialog box pops up. In order to be consistent with other Qgis Plug-ins, the author recommends the use of the Qgis plug-in standard location, namely:

c:\users\< your username >\.qgis2\python\plugins

Of course, you can not use this location, but you need to use the "Qgis_pluginpath" This environment variable to specify your plug-in location, otherwise the QGIS software can not load the plug-in. The configuration method can be written to the "startup.py" file before the Qgis startup, or it can be configured in the Qgis software using the settings-> Options-> System-> environment. The concrete method is not expanded here.

Finally, click on the "OK" button, our plug-in project is created well.

Compiling plug-in engineering

The plug-in project generated using the Plugin Builder is directly available for compilation. Next, we need to use the "PYRCC4" tool to compile the project.

"Pyrcc4" is a command-line tool, and there are two ways to find him. The first way is to use the command-line tool directly under Windows, and if you enter "PYRCC4" to eject the information shown in the following illustration, this means that the tool is available.

Some friends can't find this tool directly using the command line, and you can find it by configuring the system PATH environment variable. Alternatively, find the "Osgeo4w.bat" in the Qgis installation directory, double-click to open it, pop up a command line window, and, in the same way, enter the "PYRCC4" test to be used.


In the Command Line window, we use the "CD" command to set the current path to our plug-in engineering path, with specific instructions:

CD c:\users\< your username >\.qgis2\python\plugins

Then use the following command to compile the resource for this plug-in.

Pyrcc4-o resources.py RESOURCES.QRC

Here, Myplugin This plugin can be opened in the Qgis software, only need to restart the Qgis can use it.

Plug-in UI Design

After successfully creating and running our first Qgis plugin, let's take a look at the design part of the UI for this plugin.

In our new plugin directory, there is a file called "Myplugin_dialog_base.ui", which is a QT UI file.

If you have QT Designer, you can double-click the file directly, and the Qt UI Designer window pops up, and the UI is designed with drag and drop.

It doesn't matter if you don't have QT designer, you can use Notepad or any text editor to open the "Myplugin_dialog_base.ui" file, where I use the Atom editor, as shown below. As you can see, essentially, this is an XML file.

To keep it simple, I used the QT Designer tool, dragged a label control into the UI window, and wrote a word.

If you are using a text editor to open the UI file, then copy the following code and replace the code in your UI file to get the same effect.

<?xml version= "1.0" encoding= "UTF-8"?> <ui version= "4.0" > <class>MyPluginDialogBase</class> <widget class= "Qdialog" name= "Myplugindialogbase" > <property name= "Geometry" > <rect> <x>0 </x> <y>0</y> <width>390</width>  

After saving the edited UI file, we need to compile it using the "pyuic4" command, enter the command in the Command line window just now, and hit enter to confirm:

Pyuic4-o ui_myplugin.py Myplugin_dialog_base.ui

Note that the "ui_myplugin.py" in this command is the output file, and if your plugin is called another name, such as "Testplugin", then you should fill in the "ui_testplugin.py" with the "ui_" prefix.

If the error is not prompted, the compilation is successful. At this point, you can use the "Plugin reloader" tool in the previous section "pyqgis– environment" to overload the plug-in (this tool is also downloaded from the Manage and Install Plugins window).

Or if you don't have the tool, restarting the Qgis effect is the same. Clicking on the "Myplugin" plugin again will see that the UI has become the way we just designed it.

problems that may be encountered

1, if your plug-in does not appear in the "Plugins" menu bar, on the one hand to check the location of plug-ins in the Qgis default plug-in path, that is,:c:\users\< your username >.qgis2\python\plugins. On the other hand, if you are sure to set the plug-in location to the "Qgis_pluginpath" variable. If you are sure, but you still cannot find the plug-in, you can open the Mange and Install Plugins window and search for your plug-in name, as shown in the following illustration, where an error occurred during the plug-in configuration. Then go back and check if the steps described above are wrong.

2, pay special attention to the "PYRCC4" and "Pyuic4" two tools in the "-o" option followed by the first parameter is the output file name, the second parameter is the input file name. And the final UI file that is generated must be prefixed with the "ui_" prefix, otherwise qgis cannot load the plug-in correctly.

OK, this is the content of this section, thank you for reading.

If there are any mistakes, please criticize them.

Qgis two times Development group: 68016799, welcome everyone to exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.