2

I write a Standalone C++ Application and I can not load a vector layer. This is the error:

QObject::connect: Cannot connect (null)::raiseError( QString ) to QgsVectorLayer::raiseError( QString )

Now, this is my code for application:

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

#include "qgsapplication.h"

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

  QString pluginPath = "/home/scit/dev/cpp/QGIS/build-master/plugins";
  QString prefixPath = "/home/scit/dev/cpp/QGIS/build-master";
  qDebug() << "Step 1";
  QgsApplication::setPluginPath( pluginPath );
  QgsApplication::setPrefixPath( prefixPath, true);
  QgsApplication a(argc, argv, true);
  a.initQgis();

  qDebug() << a.showSettings();

  MainWindow w;
  w.show();

  return a.exec();
}

Where "/home/scit/dev/cpp/QGIS/build-master/plugins" you can find all *.so libraries for plugin of a build of QGIS.

this->mapCanvas = new QgsMapCanvas(ui->centralWidget, "The_Canvas");
ui->gridLayout->addWidget( this->mapCanvas, 0, 0, 2, 1 );

....

QgsVectorLayer *layer = new QgsVectorLayer( "/home/scit/Descargas/ne_10m_populated_places.shp", base, "ogr", false );

Q_CHECK_PTR( layer );

if ( layer->isValid() ){
    qDebug() << "VALID";
    this->mapCanvas->setExtent(layer->extent());
    QList<QgsMapLayer*> myList;
    myList << layer;
    QgsMapLayerRegistry::instance()->addMapLayers( myList );
    this->mapCanvas->freeze(false);
    this->mapCanvas->refresh();
}else{
    qDebug() << "INVALID";
}

And my .pro for QT project

QT       += core gui network sql xml

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = qgisExtension
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/ -lqgis_core
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/ -lqgis_analysis
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/ -lqgis_app
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/ -lqgis_gui
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/ -lqgispython
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/ -lqgis_networkanalysis

#I do not know if this is necessary!!!!!
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libbasicauthmethod.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgeometrycheckerplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgrassprovider6.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libmssqlprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libpkipathsauthmethod.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libspatialiteprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libwmsprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libcoordinatecaptureplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgeometrysnapperplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgrassrasterprovider6.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libofflineeditingplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libpostgresprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libspatialqueryplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libzonalstatisticsplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libdelimitedtextprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgeorefplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libheatmapplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libogrprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libqgisgrass6.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libtopolplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libdxf2shpconverterplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgpsimporterplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libidentcertauthmethod.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/liboracleplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libqgisgrass6.so.2.14.5
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libvirtuallayerprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libevis.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgpxprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libinterpolationplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libowsprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/librasterterrainplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libwcsprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgdalprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libgrassplugin6.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libmemoryprovider.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libpkcs12authmethod.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libroadgraphplugin.so
LIBS += -L$$PWD/../../../cpp/QGIS/build-master/output/lib/qgis/plugins/libwfsprovider.so


INCLUDEPATH += $$PWD/../../../cpp/QGIS/src/core
DEPENDPATH += $$PWD/../../../cpp/QGIS/src/core

INCLUDEPATH += $$PWD/../../../cpp/QGIS/src/core/geometry
DEPENDPATH += $$PWD/../../../cpp/QGIS/src/core/geometry

INCLUDEPATH += $$PWD/../../../cpp/QGIS/src/gui
DEPENDPATH += $$PWD/../../../cpp/QGIS/src/gui

INCLUDEPATH += $$PWD/../../../cpp/QGIS
DEPENDPATH += $$PWD/../../../cpp/QGIS

QMAKE_CXXFLAGS += -std=c++0x
DEFINES += GUI_EXPORT= CORE_EXPORT=
1

The error is on the paths, and the absence of environment variables. I needed set to LD_LIBRARY_PATH And the pluginPath, set it to the path QGIS plugins. I'm working with a compilation of QGIS within path $HOME/apps/qgis

Therefore: LD_LIBRARY_PATH = $HOME/apps/qgis/lib

QString pluginPath = "$HOME/apps/qgis/lib/qgis/plugins";
QgsApplication :: setPluginPath (pluginPath);
QgsApplication a (argc, argv, true);
a.initQgis ();

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.