3

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:

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?

1
  • OK. I replaced images with text. Commented Jun 16, 2020 at 15:47

1 Answer 1

2

The problem is solved. I think the problem was about setting the required environment variables by QGIS.

I used the following bat file when launching Qt Creator. This fixed my problem.

PATH=C:\OSGEO4~1\apps\qgis-ltr-dev\bin;C:\OSGEO4~1\apps\gdal-dev\pymod3\scripts;C:\OSGEO4~1\apps\Python37;C:\OSGEO4~1\apps\Python37\Scripts;C:\OSGEO4~1\apps\gdal-dev\bin;C:\OSGEO4~1\apps\proj-dev\bin;C:\OSGEO4~1\apps\qt5\bin;C:\OSGEO4~1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBem;%PATH%
set GDAL_DATA=C:\OSGEO4~1\apps\gdal-dev\share\gdal
set GDAL_DRIVER_PATH=C:\OSGEO4~1\apps\gdal-dev\bin\gdalplugins
set GDAL_FILENAME_IS_UTF8=YES
set GEOTIFF_CSV=C:\OSGEO4~1\share\epsg_csv
set JPEGMEM=1000000
set OSGEO4W_ROOT=C:\OSGEO4~1
set PROJ_LIB=C:\OSGEO4~1\apps\proj-dev\share\proj
set PYTHONHOME=C:\OSGEO4~1\apps\Python37
set PYTHONPATH=C:\OSGEO4~1\apps\gdal-dev\pymod3\lib\site-packages;C:\OSGEO4~1\apps\Python37;C:\OSGEO4~1\apps\Python37\Scripts
set QGIS_PREFIX_PATH=C:/OSGEO4~1/apps/qgis-ltr-dev
set QT_PLUGIN_PATH=C:\OSGEO4~1\apps\qgis-ltr-dev\qtplugins;C:\OSGEO4~1\apps\qt5\plugins
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set O4W_QT_PREFIX=C:/OSGEO4~1/apps/Qt5
set O4W_QT_BINARIES=C:/OSGEO4~1/apps/Qt5/bin
set O4W_QT_PLUGINS=C:/OSGEO4~1/apps/Qt5/plugins
set O4W_QT_LIBRARIES=C:/OSGEO4~1/apps/Qt5/lib
set O4W_QT_TRANSLATIONS=C:/OSGEO4~1/apps/Qt5/translations
set O4W_QT_HEADERS=C:/OSGEO4~1/apps/Qt5/include

start "Qt Creator" /B  D:\Development\Qt\Qt5.12.0\Tools\QtCreator\bin\qtcreator.exe %*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.