I'm trying to develop a custom GIS application using Qt. Which will show a map and have some figure drawing options on the map. While searching on the web, I came across with QGIS. I wonder if I could use QGIS C++ API (QgsMapCanvas etc.) for basic map functionality in my app.
I wrote a simple Qt Application to use QGIS API. It is compiled and linked successfully. But when I tried to run the application it gives: " The program has unexpectedly finished." error. And crashes.
My development environment:
- Qt 5.12.0
- MSVC 2015
- Windows 10 x64
- OSGeo4W64 is installed including "qgis-ltr-dev" package (QGIS version 3.11.0-8) http://download.osgeo.org/osgeo4w/osgeo4w-setup-x86_64-v1.exe (64bit) (link updated - 2018-Feb-26)
My pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2020-06-10T09:28:31
#
#-------------------------------------------------
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Hello_QGis_2
TEMPLATE = app
CONFIG += c++11
SOURCES += main.cpp
INCLUDEPATH += C:\OSGeo4W64\apps\qgis-ltr-dev\include
LIBS += -L"C:\OSGeo4W64\apps\qgis-ltr-dev\lib" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Source Code (main.cpp):
#include <QApplication>
#include <qgsapplication.h>
#include <QtDebug>
int main(int argc, char *argv[])
{
QgsApplication app(argc, argv, true);
qDebug() << "*** QGS Application Instance Created *** ";
qDebug() << "*** setPrefixPath to C:\\OSGeo4W64\\apps\\qgis-ltr-dev *** ";
QString PREFIX_PATH = "C:\\OSGeo4W64\\apps\\qgis-ltr-dev";
QgsApplication::setPrefixPath(PREFIX_PATH);
qDebug() << "*** initQgis ***";
QgsApplication::initQgis();
int exitCode= app.exec();
QgsApplication::exitQgis();
qDebug() << "*** Finished *** ";
return exitCode;
}
I also used following bat file to start Qt Creator: (As mentioned in QT C++ application using QGIS API)
ECHO Setting up QGIS DEV ENV
set PYTHONPATH=
set OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
@set QMAKESPEC=win64-msvc2015
@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:\OSGeo4W64\apps\Qt5\bin;C:\OSGeo4W64\apps\Qt5\Tools\QtCreator\bin;%PATH%
set VS140COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
start "Qt Creator" /B D:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\qtcreator.exe %*
As I read from the documentation, the library files for Windows (*.dll) in OsGeo4W64 produced using MSVC 2015. And I tried to use those libraries with the same compiler. So I can not figure out what am I missing?