4

I'm trying to develop an Standalone Application with Qt and QGIS API.

There are several code examples that should work, but they don't.

The Problem is that everytime I use "QgsApplication" the application Output says: "The program has unexpectedly finished."

Even this simple code does not work:

#include "mainwindow.h"
#include <QApplication>

#include "qgsapplication.h"

int main(int argc, char *argv[])
{
    QgsApplication app(argc, argv, true);
}  

My Project.pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Project
TEMPLATE = app


SOURCES += main.cpp\
    mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

LIBS += -L"C:/QGIS Building/lib" -lqgis_core -lqgis_gui

INCLUDEPATH += "C:/QGIS Building/include"

DEFINES +=      CORE_EXPORT=""
DEFINES +=      GUI_EXPORT=""
5
  • 1
    Have you tried a debugger? How are you linking this?
    – BradHards
    Dec 16, 2014 at 20:59
  • No I haven't tried a Debugger yet. I added the linking to my question.
    – Niko
    Dec 17, 2014 at 6:30
  • have you resolved this issue? Nov 26, 2015 at 10:44
  • Yes I did. In my case it was just a bug in the QGis version I downloaded.
    – Niko
    Nov 28, 2015 at 11:44
  • As June 2020 I have very similar question at: gis.stackexchange.com/questions/365057/… Jun 16, 2020 at 14:40

1 Answer 1

2

I had similar problems, when I try to run a standalone QGis App. Important is that you call QApplication at first and then set context path for QGIS with

   QgsApplication::setPrefixPath("/usr", true);

(in LINUX) before you call.

   QgsApplication::initQgis();

A program initialisation that works for me looks like this.

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

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

   // 1. Initialize App
   QApplication app(argc, argv);
   QgsApplication::setPrefixPath("/usr", true);
   QgsApplication::initQgis();

    //2. App start
    MainWindow win(config, db);
    win.show();
    int result = app.exec();

    //3. Qgis free libs
    QgsApplication::exitQgis();

    //4. And say goodby
    return result;
 }
3
  • It's true what you say, it has to be coded in this order. But my Problem was a bug in the QGis Version, after I tried the latest version all my problems were gone.
    – Niko
    Apr 14, 2015 at 14:22
  • sir how you are creating this application Like its simple visual studio project in which you are including qgis librarie? Nov 26, 2015 at 11:15
  • I guess, the library declarion follows the qmake syntax en.wikipedia.org/wiki/Qmake, not visual studio configs.
    – huckfinn
    Nov 30, 2015 at 22:45

Your Answer

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

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