The C++ plugin example covered in this manual is a point converter plugin and intentionally kept simple. The plugin searches the active vector layer in QGIS, converts all vertices of the layer’s features to point features (keeping the attributes), and finally writes the point features to a delimited text file. The new layer can then be loaded into QGIS using the delimited text plugin (see User Manual).
本手册中的C++插件示例是点转换器插件,并有意保持简单。该插件搜索QGIS中的活动矢量图层,将图层要素的所有顶点转换为点要素(保留属性),最后将点要素写入分隔文本文件。然后可以使用分隔文本插件将新图层加载到QGIS中(参见用户手册)。
Step 1: Make the plugin manager recognise the plugin 第一步:让插件管理器识别插件
As a first step we create the QgsPointConverter.h and QgsPointConverter.cpp files. We then add virtual methods inherited from QgisPlugin (but leave them empty for now), create the necessary external ’C’ methods, and a .pro file (which is a Qt mechanism to easily create Makefiles). Then we compile the sources, move the compiled library into the plugin folder, and load it in the QGIS Plugin Manager.
作为第一步,我们创建QgsPointConverter.h和QgsPointConverter.cpp文件。然后,我们添加从QgisPlugin继承的虚拟方法(但暂时将它们保留为空),创建必要的外部“C”方法和一个.pro文件(这是一个易于创建makefile的Qt机制)。然后我们编译源代码,将编译后的库移动到plugin文件夹中,并将其加载到QGIS插件管理器中。
a) Create new pointconverter.pro file and add:
#base directory of the qgis installation
QGIS_DIR = /home/marco/src/qgis
TEMPLATE = lib
CONFIG = qt
QT += xml qt3support
unix:LIBS += -L/$$QGIS_DIR/lib -lqgis_core -lqgis_gui
INCLUDEPATH += $$QGIS_DIR/src/ui $$QGIS_DIR/src/plugin
s $$QGIS_DIR/src/gui \
$$QGIS_DIR/src/raster $$QGIS_DIR/src/core $$QGIS_DIR
SOURCES = qgspointconverterplugin.cpp
HEADERS = qgspointconverterplugin.h
DEST = pointconverterplugin.so
DEFINES += GUI_EXPORT= CORE_EXPORT=
b) Create new qgspointconverterplugin.h file and add
:
#ifndef QGSPOINTCONVERTERPLUGIN_H
#define QGSPOINTCONVERTERPLUGIN_H
#include "qgisplugin.h"
/**A plugin that converts vector layers to delimited text po
int files.
The vertices of polygon/line type layers are converted to po
int features*/
class QgsPointConverterPlugin: public QgisPlugin
{
public:
QgsPointConverterPlugin(QgisInterface* iface);
~QgsPointConverterPlugin();
void initGui();
void unload();
private:
QgisInterface* mIface;
};
#endif
c) Create new qgspointconverterplugin.cpp file and add:
#include "qgspointconverterplugin.h"
#ifdef WIN32
#define QGISEXTERN extern "C" __declspec( dllexport )
#else
#define QGISEXTERN extern "C"
#endif
QgsPointConverterPlugin::QgsPointConverterPlugin(Qg
isInterface* iface): mIface(iface)
{
}
QgsPointConverterPlugin::~QgsPointConverterPlugin()
{
}
void QgsPointConverterPlugin::initGui()
{
}
void QgsPointConverterPlugin::unload()
{
}
QGISEXTERN QgisPlugin* classFactory(QgisInterface* ifa
ce)
{
return new QgsPointConverterPlugin(iface);
}
QGISEXTERN QString name()
{
return "point converter plugin";
}
QGISEXTERN QString description()
{
return "A plugin that converts vector layers to delimited te
xt point files";
}
QGISEXTERN QString version()
{
return "0.00001";
}
// Return the type (either UI or MapLayer plugin)
QGISEXTERN int type()
{
return QgisPlugin::UI;
}
// Delete ourself
QGISEXTERN void unload(QgisPlugin* theQgsPointConverte
rPluginPointer)
{
delete theQgsPointConverterPluginPointer;
}
Step 2: Create an icon, a button and a menu for the plugin
This step includes adding a pointer to the QgisInterface object in the plugin class. Then we create a QAction and a callback function (slot), add it to the QGIS GUI using QgisInterface::addToolBarIcon() and QgisInterface::addPluginToMenu() and finally remove the QAction in the unload() method.
d) Open qgspointconverterplugin.h again and extend existing content to:
#ifndef QGSPOINTCONVERTERPLUGIN_H
#define QGSPOINTCONVERTERPLUGIN_H
#include "qgisplugin.h"
#include <QObject>
class QAction;
/**A plugin that converts vector layers to delimited text po
int files.
The vertices of polygon/line type layers are converted to po
int features*/
class QgsPointConverterPlugin: public QObject, public Qg
isPlugin
{
Q_OBJECT
public:
QgsPointConverterPlugin(QgisInterface* iface);
~QgsPointConverterPlugin();
void initGui();
void unload();
private:
QgisInterface* mIface;
QAction* mAction;
private slots:
void convertToPoint();
};
#endif