[Qgis-user] QGIS plugin: very basic information

Goyo goyodiaz at gmail.com
Wed May 13 11:52:19 PDT 2015


Hello Filippo,

I will try to answer your questions (inline) but I feel they are too
broad to provide meaningful answers. I suggest you try to be more
concise and focus in more specific issues.

2015-05-13 9:00 GMT+02:00 Filippo Gualla <filippog16 at gmail.com>:
> Hi everybody,
>
> my name is Filippo and I am new in this forum. For my thesis I am trying to
> develop a QGIS plugin with Python. I have already read many stuff from the
> web (Python cookbook, all the official web-pages and other blog) and then I
> started to go through some plugin downloaded from the repository and compare
> them (I do not know if I am allowed to write with which plugin I am doing my
> comparisons). I have already used PluginBuilder to make the starting files
> (py, ui, ...) and I have created the GUI from PyQt and translated the ui
> file into the py file via Prompt MS DOS. But now I have some problem to
> understand the "frame" behind a plugin.
> Here some very general and basic questions (I need to stress that on the web
> I haven´t found any source which goes in detail to the basics):
>
> 01 - In general, in my plugin I need to import a raster DEM and a vector
> layer + some parameters. In my GUI (from PyQt) I created the places where
> insert vector, DEM and variables. Each of this spaces has its own proper
> name that should be (as I understood) entered in the main py file script (I
> checked my "ui_<name>.py" file and as expected I found the name I entered in
> PyQt). Now my question is: where (in which method in the main py file) and
> how I recall my variables and parameters(within __init__ or in the method
> which perform the analysis)? I ask because it is not clear where I access my
> input data (in one plugin I compared I assume it was from __init__, in
> another only in the method that perform the analysis... I understand that
> maybe these differences are due to different needs, but I wonder which is
> the easiest way, now I do not need something perfect from a "script" point
> of view;

I am not sure I understand the question. There is no single place or
way to get or access input data. That's not something the plugin
framework imposes on you but something you have to decide as the
software programmer/designer.

> 02 - It is unclear the relationship among all the methods within the main py
> file (aka the class defined in the main py file), like __init__, initGUI +
> all the others. Some of them are default once the user create the files with
> PlugIn Builder but I assume there is something more to know because most of
> the plugin show the default and others, an other plugin only __init__ and
> initGUI. In any case in my mind I figured out a method which recall all the
> data from the GUI through the name defined in PyQt, then another def method
> which takes them as input and perform the analysis and finally another
> method to show/print the result, but still I do not get the relationship
> among all those methods (I have already written some script both with python
> and matlab and now in my mind the process in quite straightforward but here
> in the plugin, involving a class and method I hardly understand the frame);

The plugin framework requires you to provide a few files, classes and
methods (described in the docs, I do not remember all the details).
Other than that you are free to to write whatever classes and methods
you want.

The plugin builder will make some opinionated choices for you, but
they are choices, not requirements. That's why some plugins may have a
different structure.

> 03 - (more theoretical) I know that on OO paradigm I can define a class and
> several method within that class. I have created some very simple classes
> and in the __init__ method I set some arguments, e.g. two (I save the class
> as a py file). Then I recall that py file in the IDLE I set something like
> var = <class_name>(arg1, arg2). That´s clear BUT when I look into a QGIS
> plugin, some times I read:
>
> def __init__(self, iface):
>         self.iface = iface
>         self.canvas = self.iface.mapCanvas()
>
> "Canvas" is not listed in the arguments of __init__, why? I assume is a sort
> of "property" of the self object.
>
> [in detail here, what is "mapCanvas"? Is it a method of the object "iface"?
> How the developer knew that "mapCanvas" had to be invoked? I ask because if
> I go through the API classes of QGIS there are thousand of them and I do not
> know where to start to look for]

iface is an instance of QgisInterface, mapCanvas() is a method of
QgisInterface. This is in the docs (feel free to ask for pointers if
you are unable to find it). We cannot know for sure how the developer
learnt about it. I guess he wanted to do something with the map canvas
and then looked for it in the docs or in the Internet or looked at a
plugin which does something similar or just asked someone else.

> 04 - In the main py file of a plugin (usually the name of the plugin) I
> always find a class definition. What I do not understand is when I recall
> this class? As written above (question 03) when I create my class in a py
> file, I can use it via IDLE. Can someone explain me which is the logic path
> in recalling the classes (also in other file like "...dialog.py" a class is
> defined BUT this class is imported finally in the main py file)

The plugin class must have certain methods (__init__, initGui, unload)
and is intended to be instantiated by the classFactory() function in
__init__.py, beyond this you can use it in whatever ways fit your
purpose but usually you do not have to. You can (and usually you'll
have to) write additional modules, classes, methods and functions. So
"the logic path in recalling the classes" is up to you.

Regards

Goyo



More information about the Qgis-user mailing list