2017-06-17 302 views
0

我想開發一個使用QGIS API的QT C++應用程序,但我無法管理Qt Creator來編譯代碼。我已經閱讀了gis.stackexchange和其他來源的幾個主題。信息大多是過時的,我還沒有找到解決方案。我需要的是關於以下項目的兼容性的總體指導方針,幫助我找到正確的組合:解決:使用QGIS API的QT C++應用程序

  • Qt版本:試過4.8.6和5.9。
  • Qt編譯器:試過MinGW,VS2008 Express(Qt 4.8.6)和VS Community 2017(Qt 5.9)
  • QGIS版本:嘗試過2.14到2.18。
  • QGIS編譯:我只使用常規OSGeo4w安裝程序,AFAIK QGIS使用Qt4和MSVC編譯。我試圖用MinGW從源代碼編譯QGIS,但沒有成功(再次,版本兼容性問題)。
  • Windows版本:我正在使用Windows 10 64位。 OSGeo4W或OSGeo4W64。

的Qt工程確定在它自己的圖書館每一個案件,但是當我嘗試導入QGIS庫它給建築物時,即使智能感知工作正常,因爲它承認QgsApplication作爲qgsapplication成員的「未定義的引用」錯誤。H。

以下只是一個測試我無法工作。我的親文件是:

QT  += core gui xml 

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 

TARGET = QgisWindow 
TEMPLATE = app 

DEFINES += QT_DEPRECATED_WARNINGS 

SOURCES += \ 
     main.cpp \ 
     mainwindow.cpp 

HEADERS += \ 
     mainwindow.h 

FORMS += \ 
     mainwindow.ui 

# QGIS 
INCLUDEPATH += "C:/OSGeo4W64/apps/qgis-rel-dev/include" 
INCLUDEPATH += "C:/OSGeo4W64/include" 

LIBS += -L"C:/OSGeo4W64/apps/qgis-rel-dev/lib" -lqgis_core -lqgis_gui 

DEFINES+=CORE_EXPORT= 
DEFINES+=GUI_EXPORT= 
# QGIS 

main.cpp中是:

#include <QApplication> 
#include <qgsapplication.h> 


int main(int argc, char *argv[]) 
{ 
    QgsApplication app(argc, argv, true); 
    QgsApplication::setPrefixPath("C:/OSGeo4W64/apps/qgis-dev", true); 

    int retval = app.exec(); 
    QgsApplication::exitQgis(); 
    return retval; 
} 

然後它不能建立/編譯蒙山此錯誤:

main.cpp:20: error: undefined reference to `QgsApplication::QgsApplication(int&, char**, bool, QString const&, QString const&)' 

我其實是一個移植PyQgis應用程序,我已經做了兩年前,因爲我相當新的Qt造物主我卡在他開始。

+0

它只是一個C++的問題,它意味着沒有定義類的類型。這需要包括在內。如果這不在qgsapplication.h中,那麼我們不知道。也許有一個需要提供的名稱空間。這是一個編程語言的問題層面。除此之外,沒有人應該提供項目的絕對路徑。 – AlexanderVX

回答

0

我發現上述項目的正確組合,以使人們有可能對Qt的進口QGIS庫和構建應用程序:

  • 的Qt 4.8.6 MSVC2010
  • Qt Creator的4.3.0(從Qt 5.9.0安裝)。 Qt Creator 3.0也可以使用。
  • 的Visual C++快訊2010年 - 包括Windows套件/ SDK中安裝
  • OSGeo4W - 安裝QGIS-LTR-dev的所有庫
  • 的CMake 2.8.2

要啓動Qt Creator中我用這個。蝙蝠:

Creator.bat

ECHO Setting up QGIS DEV ENV 

set PYTHONPATH= 

set OSGEO4W_ROOT=C:\OSGeo4W 
call "%OSGEO4W_ROOT%\bin\o4w_env.bat" 

@set QMAKESPEC=win32-msvc2010 

@set PATH=%OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\bin;%PATH% 

@set INCLUDE=%INCLUDE%;%OSGEO4W_ROOT%\include;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\include 

@set LIB=%LIB%;%OSGEO4W_ROOT%\lib;%OSGEO4W_ROOT%\apps\qgis-ltr-dev\lib 

path %OSGEO4W_ROOT%\bin;%SYSTEMROOT%\System32;%SYSTEMROOT%;%SYSTEMROOT%\System32\wbem;C:\Program Files (x86)\Git\bin;C:\Tools\QtCreator\bin;%PATH% 

set VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools 
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86 

start "Qt Creator" /B C:\Qt\Qt5.9.0\Tools\QtCreator\bin\qtcreator.exe %* 

PRO文件:

QT  += core gui xml 

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 

TARGET = 1_hello_world_qgis_style 
TEMPLATE = app 

DEFINES += QT_DEPRECATED_WARNINGS 

SOURCES += main.cpp 

## QGIS 
INCLUDEPATH += "C:/OSGeo4W/include" 
INCLUDEPATH += "C:/OSGeo4W/apps/qgis-ltr-dev/include" 

LIBS += -L"C:/OSGeo4W/apps/qgis-ltr-dev/lib" -lqgis_core -lqgis_gui 

DEFINES+=CORE_EXPORT= 
DEFINES+=GUI_EXPORT= 
## QGIS 

爲主。CPP文件(改編自Tim Sutton's examples

// Qt Includes 
#include <QApplication> 

// QGIS Includes 
#include <qgsapplication.h> 
#include <qgsproviderregistry.h> 
#include <qgssinglesymbolrendererv2.h> 
#include <qgsmaplayerregistry.h> 
#include <qgsvectorlayer.h> 
#include <qgsmapcanvas.h> 

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

    // Start the Application 
    QgsApplication app(argc, argv, true); 

    app.setPrefixPath("D:/GIS"); 

    QString myPluginsDir  = "<path to Qgis plugins dir>"; 
    QString myLayerPath1   = "<path to shapefile 1>"; 
    QString myLayerBaseName1  = "Layer1"; 
    QString myLayerPath2   = "<path to shapefile 2>"; 
    QString myLayerBaseName2  = "Layer2"; 
    QString myProviderName  = "ogr"; 

    // Instantiate Provider Registry 
    QgsProviderRegistry::instance(myPluginsDir); 
    // create maplayer instances 
    QgsVectorLayer * mypLayer1 = new QgsVectorLayer(myLayerPath1, myLayerBaseName1, myProviderName); 
    QgsVectorLayer * mypLayer2 = new QgsVectorLayer(myLayerPath2, myLayerBaseName2, myProviderName); 
    QgsSingleSymbolRendererV2 *mypRenderer1 = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer1->geometryType())); 
    QgsSingleSymbolRendererV2 *mypRenderer2 = new QgsSingleSymbolRendererV2(QgsSymbolV2::defaultSymbol(mypLayer2->geometryType())); 
    QList <QgsMapCanvasLayer> myLayerSet; 
    mypLayer1->setRendererV2(mypRenderer1); 
    mypLayer2->setRendererV2(mypRenderer2); 

    // Add Vector Layers to the Layer Registry 
    QgsMapLayerRegistry::instance()->addMapLayer(mypLayer1, true); 
    QgsMapLayerRegistry::instance()->addMapLayer(mypLayer2, true); 
    // Add Layers to the Layer Set 
    myLayerSet.append(QgsMapCanvasLayer(mypLayer1, true)); 
    myLayerSet.append(QgsMapCanvasLayer(mypLayer2, true)); 

    // Create the Map Canvas 
    QgsMapCanvas * mypMapCanvas = new QgsMapCanvas(0, 0); 
    mypMapCanvas->setExtent(QgsRectangle(-63.50, -28.10, -58.33, -24.00)); // Chaco 
    mypMapCanvas->setWheelAction(QgsMapCanvas::WheelAction(2) , 1.2); 
    mypMapCanvas->enableAntiAliasing(true); 
    mypMapCanvas->setCanvasColor(QColor(255, 255, 255)); 
    mypMapCanvas->freeze(false); 
    // Set the Map Canvas Layer Set 
    mypMapCanvas->setLayerSet(myLayerSet); 
    mypMapCanvas->setVisible(true); 
    mypMapCanvas->setWindowTitle("GIS DSH"); 
    mypMapCanvas->refresh(); 

    // Start the Application Event Loop 
    int retval = app.exec(); 

    app.exitQgis(); 

    return retval; 
} 

路徑是硬編碼的(不好的編程習慣),但僅僅是這個測試。

現在它運行良好。請看,我的帖子在gis.stackexchange.com的鏈接和Qt Creator Build &的運行配置。