qgis二次开发环境搭建(超级详细)

      最近有一个项目要求基于qgis+QT进行二次开发开发,要使用到qgis平台。陆陆 续续花了差不多两个多星期,在把开发环境搭建起来。

       首先就面临2种选择,一种 到qgis官网下载源码,进行编译,此 情况我走了一般编译成功,但是很麻烦,还有很多问题没有解决。

       另外,直接在线下载, 官方网站 编译好的二次开发SDK包。本篇 文章采用的是第2种方法,简单快捷,但是也遇到各种各样的问题,现记录如下,尽可能的详细。期间看过很多大神的博客,在此表示感谢。

一,开发环境《注意很重要,我就是在此没有注意,遇到各种坑,耽误不少时间》

       VS2015 + QT5.10.1 + OSGeo4W

       注意,QT 平台的版本与qgis下载的版本有关,本文采用QT是32位, 通过OSGeo4w下载也是qgis也是32位,如果使用qt是64位,那么应该使用OSGeo4w64来下载qgis;

     如果下载qgis-ltr(官网的长期支持版,qgis 2.18),那么建议qt采用5.0以下的版本;

     如果下载qgis 是3.0版本以上,个人建议qt版本,下载高于qt5.9.2

     另外如果是使用VS2013,那么好像不能编译qgis3.0版本以上的,c++11个部分语法报错。好了不啰嗦了,正式介绍了。

二,关于qgis二次开发SDK下载

     官网网站 https://www.qgis.org/en/site/  

     下载OSGeo4w  https://www.qgis.org/en/site/forusers/download.html   自己选择32位还是64位, 这个软件的下载速度很慢,要有耐心,反正在帝都只有一般20-40kb/s,还容易出错。

     osgeo4w-setup-x86.exe ,安装使用默认路径,直接上图

    1 选择Andvance Install

      

      2 Install  from Intenet

       

     3 中间省略几步,使用默认即可

     

    4 到这个步骤,在搜索框内输入qgis, desktop表示下载安装桌面版的qgis,lib表示我们要二次开发sdk库

      

   5 我在这里将qgis3.2版本, qgis2.18长期版都下载下来了, 实际可根据自己的情况进行选择,下载 

       过程很漫长,并且会下载很多依赖的库。很 耗时。

       

三,vs2015搭建工程

        由于我们qgis的二次开发包,是release版本,所以我们的vs工程环境,编译也应该是release版本,否则报各种错误, 

大致是debug与release版本混淆导致,错误如下图所示。

        

       对工程项目进行配置,加载第3方的 qgis库,

      1,项目工程,右键属性-->C/C++ 点击常规,附加包含目录,点击文件夹图标,添加qgis库的头文件目录,我在此处选择的是

           qgis3.2版本。

          C:\OSGeo4W\include    

          C:\OSGeo4W\apps\qgis\include

      

    2, 链接器中,常规 选择qgis的库

    

   3,链接器,输入,书写加载qgis库,

       qgis_core.lib  qgis_app.lib  qgis_gui.lib

       

4, 关于测试工程的代码,如下,非本人原创,参考其他博客得来。

       首先,main.cpp

#include "gisTest2.h"
#include <qgsapplication.h>

int main(int argc, char *argv[])
{

    QgsApplication a(argc, argv, true);
    QgsApplication::setPrefixPath("C:/OSGeo4W/apps/qgis", true);
    QgsApplication::initQgis();    //初始化QGIS应用
    gisTest2 w;    //创建一个窗体,类似于Qt
    w.show();

    return a.exec();
}
 

gisTest2.cpp代码如下:

#include "gisTest2.h"
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <qgsvectorlayer.h>

gisTest2::gisTest2(QWidget *parent)
    : QMainWindow(parent)
{
    this->resize(600, 400);

    // create the menus and then add the actions to them.
    fileMenu = this->menuBar()->addMenu("File");
    openFileAction = new QAction("Open", this);
    this->connect(openFileAction, SIGNAL(triggered(bool)), this, SLOT(on_openFileAction_triggered()));
    fileMenu->addAction(openFileAction);

    // initialize the map canvas
    mapCanvas = new QgsMapCanvas();
    this->setCentralWidget(mapCanvas);

    mapCanvas->setCanvasColor(QColor(255, 255, 255));
    mapCanvas->setVisible(true);
    mapCanvas->enableAntiAliasing(true);

}

void gisTest2::on_openFileAction_triggered() {
    addVectorLayer();
}

void gisTest2::addVectorLayer()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open shape file"), "", "*.shp");
    QStringList temp = fileName.split('/');
    QString basename = temp.at(temp.size() - 1);
    QgsVectorLayer* vecLayer = new QgsVectorLayer(fileName, basename, "ogr");

    if (!vecLayer->isValid())
    {
        QMessageBox::critical(this, "error", QString("layer is invalid: \n") + fileName);
        return;
    }
    mapCanvas->setExtent(vecLayer->extent());
    layers.append(vecLayer);
    mapCanvas->setLayers(layers);
    mapCanvas->refresh();
}

gisTest2.h代码

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_gisTest2.h"
#include <qmenu.h>
#include <qaction.h>
#include <qgsmapcanvas.h>


class gisTest2 : public QMainWindow
{
    Q_OBJECT

public:
    gisTest2(QWidget *parent = Q_NULLPTR);

private:
    // create the menus and then add the actions to them.
    QMenu *fileMenu;
    QAction *openFileAction;

    //map canvas
    QgsMapCanvas *mapCanvas;
    QList<QgsMapLayer *> layers;

    public slots:
    void on_openFileAction_triggered();
    //

public:
    void addVectorLayer();

};
 

四,工程项目编译或运行时,提示很多库找不到

       将 C:\OSGeo4W\apps\qgis\bin

          C:\OSGeo4W\bin  

      目录下dll文件拷贝到工程目录下,编译搞定,当然运行还是报错,根据错误提示将

       C:\OSGeo4W\apps\Qt5\bin  目录下的dll库拷贝,工程目录下,一直到成功编译运行成功为止。

     运行结果如下:

    关于shp 文件的数据,大家可以到  国家地理信息公共服务平台 http://www.tianditu.gov.cn/index.html 下载

        

    好了,到此初步qgis开发二次开发环境,基本搭建完成了。      

猜你喜欢

转载自blog.csdn.net/xiongjia516/article/details/81668665
今日推荐