1

I'm trying to create a simple application using QGIS 3 SDK. I have used osgeo4w-setup-x86_64.exe for installing QGIS, and I have also installed qgis-dev and qgis-ltr-dev and Qt5. I have set up my .pro file like this:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

INCLUDEPATH += "C:\OSGeo4W64\apps\qgis-ltr-dev\include" \
                "C:\OSGeo4W64\apps\qgis-ltr-dev\bin"

LIBS += -L"C:\OSGeo4W64\apps\qgis-ltr-dev\lib" -lqgis_core -lqgis_gui -lqgis_app

SOURCES += \
    main.cpp 

and the main.cpp is like this:

#include <qgsapplication.h>

#include <QDebug>
int main(int argc, char *argv[])
{
    qDebug() << "*** QGS Application Instance Created *** ";

    QgsApplication app(argc, argv, true);
    int exitCode= app.exec();

    return exitCode;
}

I run my code in this enviroment:

Windows 10 
Qt 5.15.1(MSVC 2019) 
Qt Creator 4.13.1  
QGIS 3.x.x

This code is compiled correctly. But, the problem is when I run it, the program crash even before entering the main function. When I comment the line QgsApplication app(argc, argv, true);, the crash does not happen. I believe there is some problem with linking the QGIS to my application.

How can I fix this?

Have I done something wrong?

Haven't I considered something in setting up the environment?

3
  • Not enough confident with C++ but I've made some PR to make QGIS C++ demos work with latest QGIS. Not merged and only tested on Ubuntu but could help maybe github.com/qgis/QGIS-Code-Examples/pulls
    – ThomasG77
    May 20, 2021 at 12:21
  • Thanks for your comment; this link is handy for me as a start point for using QGIS3 C++ API; I wonder why they don't update the sample code for QGIS3. May 20, 2021 at 14:29
  • It takes work and there is no one dedicated to it, the repo readme says it is there for archival reasons only. This means you can be the guy to make an awesome set of working C++ examples as you learn! Others will be able to build upon your foundation! Awesome! Dec 22, 2021 at 8:29

1 Answer 1

1

I have found the answer in here. But a new problem is raised.

I put a breakpoint on the line QgsApplication app(argc, argv, true) and the debugger pops up this:

enter image description here

And at the end of the program, when the line return app.exec(); run, this warning is displayed on the console.

QApplication::exec: Please instantiate the QApplication object first

PS: I explain the problem completely here.

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.