[GIS] How to develop C++ standalone QGIS C++ Application

cdevelopmentqgis

I want to integrate QGIS in QT\C++ application. I try to write basic C++ application to render map and build it using MSVC 2010.

class PostGisViewer: public QMainWindow
{
public:
    PostGisViewer(): QMainWindow()
    {    
        QWidget *w = new QWidget();
        this->setCentralWidget(w);
        QgsMapCanvas *canvas = new QgsMapCanvas();
        canvas->useImageToRender(false);
        QVBoxLayout *layout = new QVBoxLayout(w);
        layout->addWidget(canvas);
        QgsDataSourceURI uri;
        uri.setConnection("127.0.0.1", "5432", "gis", "login", "pass", QgsDataSourceURI::SSLdisable);
        uri.setDataSource("public", "myedges", "the_geom", "");
        QgsVectorLayer layer(uri.uri(), "testLayer", "postgres");
        QgsMapLayerRegistry::instance()->addMapLayer(&layer);
        QgsRectangle r(-1.5, -0.6, 1, 1);
        canvas->setExtent(r);
        QgsMapCanvasLayer cl(&layer);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QgsApplication::setPrefixPath("C:/Program Files/QGIS Brighton/apps/qgis", true);
    QgsApplication::initQgis();
    PostGisViewer wnd;
    wnd.show();
    int retval = a.exec();
    QgsApplication::exitQgis();
    return retval;
}

But I can't build it because I haven't qgsconfig.h and lib files. Must I build QGIS from source to create needed libs and dlls and develop C++ QGIS applications?

Best Answer

I recommend you OSGeo4W, it has all you need to build an QGis/Qt standalone application with MSVC 2010. Download it and install all the packages you need.

Then you only need to configure you project to use those tools. For example, if you use Qt Creator you need to configure the Qt version and the compiler.

  • To configure the Qt version you need to use the qmake installed with OSGeo4W
  • To configure the compiler you need to do nothing because Qt Creator auto detect the available compilers

Once you have configured you project you need to include the headers and import the right libraries. This is an example in the project file of Qt Creator:

INCLUDEPATH +=  "{ROOT OF OSGEO4W INSTALL}\include"
INCLUDEPATH +=  "{ROOT OF OSGEO4W INSTALL}\\apps\\qgis\\include"

LIBS += -L"{ROOT OF OSGEO4W INSTALL}\\apps\\qgis\\lib" -lqgis_core -lqgis_gui

DEFINES+=CORE_EXPORT=
DEFINES+=GUI_EXPORT=
Related Question