Qt4.8.6+mingw+Qgis2.4.0基于QGis的二次开发

关于QGis的二次开发,大致看了一下,基本都是在VC+QT的环境下做环境部署,并且QGis的版本号很老。在mingw下直接开发搭建好开发环境的样例少之又少。基于最新的Qgis2.4.0版本号做了对应的尝试,并成功搭建对应的环境。大致的配置例如以下:

因为在搭建的过程之中遇到的问题实在太多,没有一一记录下来。所以在本文中没有做过多的描写叙述。如有疑问能够联系husteryou@163.com。或者留言

环境搭建






pro文件配置

QT       += core gui xml

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qgisdemo
TEMPLATE = app


SOURCES += main.cpp# \
    #mainwindow.cpp

#INCLUDEPATH += qgis-2.4.0
#INCLUDEPATH += qgis-2.4.0\core
#INCLUDEPATH += qgis-2.4.0\core\symbology-ng
#INCLUDEPATH += qgis-2.4.0\analysis
#INCLUDEPATH += qgis-2.4.0\gui

INCLUDEPATH += qgis-2.4.0
INCLUDEPATH += qgis-2.4.0/core
INCLUDEPATH += qgis-2.4.0/core/symbology-ng
INCLUDEPATH += qgis-2.4.0/analysis
INCLUDEPATH += qgis-2.4.0/gui


FORMS    += mainwindow.ui

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -llibqgis_analysis
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -llibqgis_analysis
else:unix:!macx: LIBS += -L$$PWD/lib/ -llibqgis_analysis

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -llibqgis_core
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -llibqgis_core
else:unix:!macx: LIBS += -L$$PWD/lib/ -llibqgis_core

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -llibqgis_gui
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -llibqgis_gui
else:unix:!macx: LIBS += -L$$PWD/lib/ -llibqgis_gui

INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/



实例

#include <QApplication>
#include <QString>
#include <QWidget>

#include <qgis.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrendererv2.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
#include <qgsgeometry.h>

#include "mouseprocess.h"
#include "paintprocess.h"


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

    QgsApplication a( argc, argv ,true);

    //////////////////////////////////////////////////////////////////////////

    //注意这三行代码须要更改路径;
    QString myPluginsDir        ="D:/Qt/workspace/qgis-2.4.0/qgis-2.4.0/build/output/plugins";//插件路径(编译好的qgis目录下的plugins目录);
    QString myLayerPath1         ="F:/gis/data_1_3/10m_admin_0_countries.shp";//图层路径,必须设置为你电脑里面shp文件的路径,不然打不开数据;
    QString myLayerPath2         ="F:/gis/qgis_sample_data/shapefiles/airports.shp";//图层路径,必须设置为你电脑里面shp文件的路径,不然打不开数据;

    //////////////////////////////////////////////////////////////////////////
    QgsProviderRegistry::instance( myPluginsDir); //初始化插件的文件夹;

    QgsVectorLayer * mypLayer1 = new QgsVectorLayer( myLayerPath1, "myLayer1", "ogr" ); //初始化矢量图层;
    QgsVectorLayer * mypLayer2 = new QgsVectorLayer( myLayerPath2, "myLayer2", "ogr" ); //初始化矢量图层;
    QgsVectorLayer * mypLayer3 = new QgsVectorLayer(); //初始化矢量图层;
    mypLayer3->setRendererV2(QgsFeatureRendererV2::defaultRenderer(QGis::Point));

    QgsFeatureIterator iter = mypLayer2->getFeatures();
    QgsFeature feature;
    while(iter.nextFeature(feature)){
        QgsGeometry *geo = feature.geometry();
        QgsPoint point = geo->asPoint();
        //qDebug() << point.x() << point.y() << endl;
    }

    for(int index = 0; index < 100; index ++){
        QgsPoint point;

        double xmin = -4.4802e+06;
        double xmax = 4.61512e+06;
        double ymin = 1.43353e+06;
        double ymax = 6.50259e+06;

        point.setX(xmin + (xmax - xmin) * ((double) qrand()) / RAND_MAX);
        point.setY(ymin + (ymax - ymin) * ((double) qrand()) / RAND_MAX);

        QgsFeature feature;
        QgsGeometry *geo = QgsGeometry::fromPoint(point);
        feature.setGeometry(geo);
        mypLayer3->addFeature(feature);
        mypLayer3->updateFeature(feature);

        qDebug() << point.x() << point.y() << endl;
    }

    QList<QgsPoint> ring;

    //-4.4802e+06 4.61512e+06 1.43353e+06 6.50259e+06
    ring.append(QgsPoint(-4.4802e+06, 1.43353e+06));
    ring.append(QgsPoint(4.61512e+06, 6.50259e+06));
    ring.append(QgsPoint(2.61512e+06, 4.50259e+06));
    //mypLayer2->addRing(ring);

    qDebug() << mypLayer2->featureCount() << endl;

    //QgsSingleSymbolRendererV2 *mypRenderer = new QgsSingleSymbolRendererV2( mypLayer->geometryType() );
    QList <QgsMapCanvasLayer> myLayerSet;
    // mypLayer->setRenderer( mypRenderer );
    //mypLayer->setRendererV2(mypRenderer);

    QgsMapLayerRegistry::instance()->addMapLayer( mypLayer1, true );
    QgsMapLayerRegistry::instance()->addMapLayer( mypLayer2, true );
    QgsMapLayerRegistry::instance()->addMapLayer( mypLayer3, true );

    //myLayerSet.append( QgsMapCanvasLayer(mypLayer3, true ) );
    myLayerSet.append( QgsMapCanvasLayer(mypLayer2, true ) );
    //myLayerSet.append( QgsMapCanvasLayer(mypLayer1, true ) );

    QgsRectangle extent = mypLayer2->extent();
    qDebug() << extent.xMinimum() << extent.xMaximum() << extent.yMinimum() << extent.yMaximum() << endl;

    QgsMapCanvas * mypMapCanvas = new QgsMapCanvas( 0, 0 );
    mypMapCanvas->setExtent(mypLayer2->extent() );
    mypMapCanvas->enableAntiAliasing( true);
    mypMapCanvas->setCanvasColor( QColor(255, 255, 255 ) );
    mypMapCanvas->freeze( false );
    mypMapCanvas->setLayerSet( myLayerSet);
    mypMapCanvas->setVisible( true );
    mypMapCanvas->refresh();
    mypMapCanvas->show();

    MouseProcess *mouseProcess = new MouseProcess();
    PaintProcess *paintProcess = new PaintProcess();
    QObject::connect(mypMapCanvas, SIGNAL(xyCoordinates(QgsPoint)), mouseProcess, SLOT(xyCoordinates(QgsPoint)));
    QObject::connect(mypMapCanvas, SIGNAL(renderComplete(QPainter*)), paintProcess, SLOT(renderComplete(QPainter*)));
    return a.exec();
}




执行结果


posted @ 2015-12-19 20:24  hrhguanli  阅读(1149)  评论(0编辑  收藏  举报