【QGIS】VS2015環境下創建QGIS工程

環境

系統:win10 x64

QGIS版本:V3.12.3

VS編譯器:VS2015-x86

QT版本:5.9.9

 

步驟

1、在VS2015開發環境下,新建 Qt GUI Application 解決方法,選擇存放路徑並給新的解決方案命名。

 

2、一路NEXT。組件選擇對話框:選擇默認的三個組件(Core、GUI、Widgets)。

 

 

3、點擊 Finish,工程創建結束。

 

 

4、創建完成的解決方案。

 

 

例子代碼 

1、QGISTest.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QGISTest.h"

#include <qmenu.h>
#include <qaction.h>
#include <qgsmapcanvas.h>

class QGISTest : public QMainWindow
{
	Q_OBJECT

public:
	QGISTest(QWidget *parent = Q_NULLPTR);

private:
	Ui::QGISTestClass ui;

private:
	// create the menus and then add the actions to them.
	QMenu *fileMenu;
	QAction *openFileAction;

	//map canvas
	QgsMapCanvas *mapCanvas;
	QList<QgsMapLayer *> layers;

public slots:
	void on_openFileAction_triggered();
	//

public:
	void addVectorLayer();
};

 

2、QGISTest.cpp

#include "QGISTest.h"
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <qgsvectorlayer.h>


QGISTest::QGISTest(QWidget *parent)
	: QMainWindow(parent)
{
	//ui.setupUi(this);

	this->resize(600, 400);

	// create the menus and then add the actions to them.
	fileMenu = this->menuBar()->addMenu("File");
	openFileAction = new QAction("Open", this);
	this->connect(openFileAction, SIGNAL(triggered(bool)), this, SLOT(on_openFileAction_triggered()));
	fileMenu->addAction(openFileAction);

	// initialize the map canvas
	mapCanvas = new QgsMapCanvas();
	this->setCentralWidget(mapCanvas);

	mapCanvas->setCanvasColor(QColor(255, 255, 255));
	mapCanvas->setVisible(true);
	mapCanvas->enableAntiAliasing(true);
}

void QGISTest::on_openFileAction_triggered() 
{
	addVectorLayer();
}

void QGISTest::addVectorLayer()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Open shape file"), "", "*.shp");
	QStringList temp = fileName.split('/');
	QString basename = temp.at(temp.size() - 1);
	QgsVectorLayer* vecLayer = new QgsVectorLayer(fileName, basename, "ogr");

	if (!vecLayer->isValid())
	{
		QMessageBox::critical(this, "error", QString("layer is invalid: \n") + fileName);
		return;
	}

	mapCanvas->setExtent(vecLayer->extent());
	layers.append(vecLayer);
	mapCanvas->setLayers(layers);
	mapCanvas->refresh();
}

 

3、main.cpp

#include "QGISTest.h"
//#include <QtWidgets/QApplication>
#include <qgsapplication.h>

int main(int argc, char *argv[])
{
	QgsApplication a(argc, argv, true);
	QgsApplication::setPrefixPath("D:/Developer/QGIS/OSGeo4W/apps/qgis", true);
	QgsApplication::initQgis();    //初始化QGIS應用
	
	QGISTest w;
	w.show();
	return a.exec();
}

D:/Developer/QGIS/OSGeo4W/ 是我計算機安裝OSGeo4W的路徑,各位根據自己實際環境做相應修改。

 

編譯設置

1、包含目錄。

QGIS-Test解決方案屬性頁 -> C/C++ -> 附加包含目錄。

增加如下目錄。

D:\Developer\QGIS\OSGeo4W\apps\qgis\include
D:\Developer\QT\Qt5.9.9\5.9.9\msvc2015\include\QtXml
D:\Developer\QGIS\OSGeo4W\include

 

2、附加庫目錄。

鏈接器 -> 常規 -> 附加庫目錄。

增加如下目錄。

D:\Developer\QT\Qt5.9.9\5.9.9\msvc2015\lib
D:\Developer\QGIS\OSGeo4W\apps\qgis\lib

 

3、附加依賴項。

鏈接器 -> 輸入 -> 附加依賴項。

增加如下庫。

Qt5Core.lib
Qt5Widgets.lib
Qt5Xml.lib
Qt5Gui.lib
qgis_core.lib
qgis_gui.lib
qgis_app.lib

 

運行效果

 

感謝

qgis二次開發環境搭建(超級詳細)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章