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=""
  • 1
    Have you tried a debugger? How are you linking this? – BradHards Dec 16 '14 at 20:59
  • No I haven't tried a Debugger yet. I added the linking to my question. – Niko Dec 17 '14 at 6:30
  • have you resolved this issue? – user2174920 Nov 26 '15 at 10:44
  • Yes I did. In my case it was just a bug in the QGis version I downloaded. – Niko Nov 28 '15 at 11:44
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;
 }
  • 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 '15 at 14:22
  • sir how you are creating this application Like its simple visual studio project in which you are including qgis librarie? – user2174920 Nov 26 '15 at 11:15
  • I guess, the library declarion follows the qmake syntax en.wikipedia.org/wiki/Qmake, not visual studio configs. – huckfinn Nov 30 '15 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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