2

I'm trying to write a simple qgis c++ application (Linux Mint).

My goal is to 'talk' to QGis programmatically in C++. Plugins seem at first over-complicated, as all I need for now is to extract geometry info (no need for GUI).

Below the code I'm stuck with - from Python examples it seemed to be a good first shot. It simply outputs 'oh'. I'm getting the same result (!layer.isValid()) running the code from this pdf, after small edits to update to qgis 2.0. Tried two .shp files - one of them in fact a bunch of files exported from qgis (.shp, .shx, .dbf..).

What would be possible causes for a non-valid layer?

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

QgsApplication app(argc, argv, true);

puts ("im alive");

QgsVectorLayer layer ("/home/koscianski/Code/qgis/shapefiles/builtups.shp", "a name I like", "ogr");

//QgsVectorLayer *layer = app.addVectorLayer ("/home/koscianski/Code/qgis/shapefiles/builtups.shp", "a name I like", "OGR");

if (layer.isValid())                          
   printf ("Found %d", (int) layer.featureCount());
else
   puts ("oh");

return 0;
}
4
  • Please edit the question to contain output of your program and ask a question. As a new user please take the Tour.
    – Vince
    Commented Dec 26, 2016 at 1:39
  • Actually, doing this in Python isn't too hard. You can use Python from the qgis interpreter or even outside of qgis without using a GUI.
    – user52245
    Commented Dec 26, 2016 at 21:06
  • Thx Jesse. The trouble is, I wanna do that in C++ (let's, please, put aside the discussion on the why, that's another matter). By surfing around I know I bet other people has the same question: the "QGis Coding and Compilation Guide" has not been updated; no blogs, no clues. So I asked here. Commented Dec 26, 2016 at 21:48
  • People, ok if you feel this is off-topic but... at stackoverflow, the guys asks to make qgis development questions... [here!] (stackoverflow.com/questions/25641888/qgis-2-4-c-helloworld). That makes perfect sense to me. There are a dozen or so similar (=development) questions around this forum. Anyway, thanks for the answers (@Jesse, @ndawson) and, truly, thnx admins for your time, it's not easy to manage a site. Ah, so far no luck (thnx @ndawson). Commented Dec 27, 2016 at 19:54

1 Answer 1

0

You haven't initialized the QGIS application. Try replacing

QgsApplication app(argc, argv, true);

with

QgsApplication app(argc, argv, true);
QgsApplication::initQgis();

The initQgis step is required to properly initialize the provider registry, without which there will be no supported data formats.

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