[英]QT C++ application using QGIS API
I'm trying to develop a QT C++ application that uses QGIS API and I can't manage Qt Creator to compile the code. 我正在尝试开发使用QGIS API的QT C ++应用程序,但是我无法管理Qt Creator来编译代码。 I've read several topicts at gis.stackexchange and from other sources.
我已经在gis.stackexchange和其他来源阅读了一些主题。 The information is mostly outdated and I havent't found a solution.
该信息大部分是过时的,我还没有找到解决方案。 What I need is an overall guideline about the compatibility of the following items that help me find the right combination:
我需要的是有关以下各项的兼容性的总体指南,可帮助我找到合适的组合:
Qt works Ok in every case with it's own libraries, but when I try to import QGIS libraries it gives an 'undefined reference' error when building, even though intellisense works fine as it recognizes QgsApplication as a member of qgsapplication.h. Qt在每种情况下都可以使用其自己的库工作,但是,当我尝试导入QGIS库时,即使intellisense可以正常工作,因为它将QgsApplication识别为qgsapplication.h的成员,但它在构建时会给出“未定义的引用”错误。
The following is just a test I can't get working. 以下只是我无法正常工作的测试。 My Pro file is:
我的Pro文件是:
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 is: 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;
}
Then it fails to build/compile whith this error: 然后它无法生成/编译此错误:
main.cpp:20: error: undefined reference to `QgsApplication::QgsApplication(int&, char**, bool, QString const&, QString const&)'
I'm actually porting a PyQgis app I've made two years ago and as I'm fairly new to Qt Creator I'm stuck at he beginning. 实际上,我正在移植我两年前制作的PyQgis应用程序,由于我对Qt Creator相当陌生,所以我一开始就陷入了困境。
I found the right combination of the above mentioned items to make it possible for Qt to import Qgis libraries and build the app: 我发现上述项目的正确组合,使Qt可以导入Qgis库并构建应用程序:
To launch Qt Creator I use this .bat: 要启动Qt Creator,请使用以下.bat文件:
Creator.bat : 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 %*
The PRO file: 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
main.cpp file (adapted from Tim Sutton's examples ) main.cpp文件(改编自Tim Sutton的示例 )
// 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;
}
Paths are hardcoded (bad programming practice) but is just for this test. 路径是硬编码的(不良的编程习惯),但是仅用于此测试。
And now it runs OK. 现在它运行正常。 Please, see my post on gis.stackexchange.com for the links and the screencaps of Qt Creator Build & Run configurations.
请查看我在gis.stackexchange.com上的帖子, 以获取Qt Creator Build&Run配置的链接和屏幕截图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.