Qt Creator + QGIS 环境配置
步骤参考 https://zhuanlan.zhihu.com/p/83805300
其中main.cpp不清晰,还原后如下:
main.cpp
#include "mainwindow.h" #include <QApplication> #include <QMessageBox> #include <QDir> #define _USE_MATH_DEFINES #include <math.h> #include <qgsapplication.h> #include <qgsmapcanvas.h> #include <qgsvectorlayer.h> #include <qgsproviderregistry.h> int main(int argc, char *argv[]) { QString cpath = QDir::currentPath(); qputenv("GDAL_DATA", "C:\\OSGeo4W64\\share\\gdal"); QgsApplication a(argc, argv, true); QgsApplication::initQgis(); MainWindow w; QgsMapCanvas* canvas = new QgsMapCanvas(); w.setCentralWidget(canvas); QString path = "D:\\Tools\\Qt\\QtProjects\\qgis_try3\\ne_10m_ocean\\ne_10m_ocean.shp"; QgsVectorLayer *layer = new QgsVectorLayer(path, "ne_10m_ocean", "ogr"); if (!layer->isValid()) { QMessageBox::information(&w, "error", layer->error().message()); } QList<QgsMapLayer *> layers; layers.append(layer); canvas->setLayers(layers); w.show(); canvas->zoomToFullExtent(); return a.exec(); }