首页 > 代码库 > Qt4.8.6+mingw+Qgis2.4.0基于QGis的二次开发

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 "mainwindow.h"
#include <QApplication>
#include <qgis.h>
#include<QApplication>
#include<qgsapplication.h>
#include<qgsproviderregistry.h>
#include<qgssinglesymbolrendererv2.h>
#include<qgsmaplayerregistry.h>
#include<qgsvectorlayer.h>
#include<qgsmapcanvas.h>
#include<QString>
#include<QApplication>
#include<QWidget>
int main( int argc, char *argv[] )
{
//        QApplication a(argc, argv);
//        MainWindow w;
//        w.show();
//        return a.exec();
    QgsApplication a( argc, argv ,true);
       //QApplication a(argc, argv);
    //////////////////////////////////////////////////////////////////////////
    //注意这三行代码需要更改路径;
    QString myPluginsDir        ="D:/Qt/workspace/qgis-2.4.0/qgis-2.4.0/build/output/plugins";
    QString myLayerPath         ="F:/20140605/data_1_3/10m_admin_1_states_provinces_shp.shp";
    QString myLayerBaseName     = "Clip";
    //////////////////////////////////////////////////////////////////////////
    QgsProviderRegistry::instance( myPluginsDir); 
    QgsVectorLayer * mypLayer = new QgsVectorLayer( myLayerPath, myLayerBaseName, "ogr" ); 
    //QgsSingleSymbolRendererV2 *mypRenderer = new QgsSingleSymbolRendererV2( mypLayer->geometryType() );
    QList <QgsMapCanvasLayer> myLayerSet;
   // mypLayer->setRenderer( mypRenderer );
   //mypLayer->setRendererV2(mypRenderer);
    QgsMapLayerRegistry::instance()->addMapLayer( mypLayer, true );
    myLayerSet.append( QgsMapCanvasLayer(mypLayer, true ) );
    QgsMapCanvas * mypMapCanvas = new QgsMapCanvas( 0, 0 );
    mypMapCanvas->setExtent(mypLayer->extent() );
    mypMapCanvas->enableAntiAliasing( true);
    mypMapCanvas->setCanvasColor( QColor(255, 255, 255 ) );
    mypMapCanvas->freeze( false );
    mypMapCanvas->setLayerSet( myLayerSet);
    mypMapCanvas->setVisible( true );
    mypMapCanvas->refresh();
    mypMapCanvas->show();
    return a.exec();
}

运行结果


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