QGIS二次开发环境配置(Visual Studio 2015 + Qt 3.12 + QGIS 3.16)

QGIS二次开发环境配置(Visual Studio 2015 + Qt 3.12 + QGIS 3.16),第1张

QGIS二次开发环境配置(Visual Studio 2015 + Qt 3.12 + QGIS 3.16) 1.系统要求

Windows10 专业版

2.安装Visual Studio 2015

从Visual Studio 官网找到自己需要的版本,并根据电脑位数、语言、文件类型进行下载,我下载的是Visual Studio Professional 2015 with Update 3。下载完成后点击程序进行安装。

如果只进行QGIS二次开发的话,在安装VS2015的时候选择自定义安装,只选择"Visual C++"即可。如下图所示。

根据自己需求进行安装,等待一段时间后安装完成。 2. 安装Qt5.12

在Qt官网的开源开发页面的下面找到Qt online Installer,点击下载,然后安装。(下载需要Qt账号,需要提前申请一个账号)

点击在线安装后,根据自己需求选择安装位置等项目。在选择要下载的组件时,尽量选择LTS版本。下面是我安装时选择的组件。

其中MSVC 2015 64bit对应VS2015。如果是VS2017,可以选择MSVC 2017。

其余选项根据自己需求进行选择。等待一段时间后安装完成。

3. 安装QGIS3.16

在QGIS官网找到适合的下载版本,最好是下载QGIS in OSGeo4W在线安装器。这里以QGIS in OSGeo4W为例进行安装。

点击安装后选择Advanced Install,之后一路Next。直到选择下载网址。

选择一个网址进行下一步。然后在搜索框内输入qgis,显示所有的可安装组件,对于所有组件,都有Skip和相关版本,选择自己需要的就可以。这里是选择了所有3.16.16-1的组件。

之后进行下载,网速感人,需要相当长的时间。

4. 配置Visual Studio 2015 Qt插件

打开Visual Studio 2015,在"工具"->“扩展和更新”->“联机"中搜索"qt”,然后安装Qt插件。

重启软件后会在工具栏中找到"Qt VS Tools",点击并找到"Options"->"Versions"进行Qt配置,配置文件夹以自己安装位置为准。

5. 运行QGIS程序

新建一个Qt项目"QGISTest"。

5.1 项目文件

main.cpp

#include "QGISTest.h"
//#include 
#include 

int main(int argc, char *argv[])
{
	QgsApplication a(argc, argv, true);
    // 这里的路径要改成自己的QGIS安装路径
	QgsApplication::setPrefixPath("D:/Software/OSGeo4W/apps/qgis-ltr-dev", true);
	QgsApplication::initQgis();    //初始化QGIS应用

	QGISTest w;
	w.show();
	return a.exec();
}

QGISTest.h

#pragma once

#include 
#include "ui_QGISTest.h"

#include 
#include 
#include 

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 layers;

	public slots:
	void on_openFileAction_triggered();
	//

public:
	void addVectorLayer();
};

QGISTest.cpp

#include "QGISTest.h"
#include 
#include 
#include 
#include 


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();
}
5.2 相关配置 5.2.1 附加包含目录

点击"Qt VS Tools"->“Qt Project Settings”->“C/C++”->“常规”,在附加包含目录中加入如下目录(根据自己的安装位置进行修改)。

D:SoftwareQt5.12.12msvc2015_64includeQtXml
D:SoftwareOSGeo4Winclude
D:SoftwareOSGeo4Wappsqgis-ltr-devinclude

5.2.2 附加库目录

点击"Qt VS Tools"->“Qt Project Settings”->“链接器”->“常规”,在附加库目录中加入如下目录(根据自己的安装位置进行修改)。

D:SoftwareQt5.12.12msvc2015_64lib
D:SoftwareOSGeo4Wappsqgis-ltr-devlib

5.2.3 附加依赖项

点击"Qt VS Tools"->“Qt Project Settings”->“链接器”->“输入”,在附加依赖项中加入如下内容。

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

5.2.4 预处理器定义

点击"Qt VS Tools"->“Qt Project Settings”->“C/C++”->“预处理器”,在预处理器定义中加入"_USE_MATH_DEFINES",并与其他项用分号隔开。

5.2.5 环境变量

在系统环境变量path中加入如下目录(根据自己的安装位置进行修改)。

D:SoftwareOSGeo4Wbin
D:SoftwareOSGeo4Wappsqgis-ltr-devbin
D:SoftwareOSGeo4WappsQt5bin
D:SoftwareOSGeo4Wappsgdal-devbin
D:SoftwareOSGeo4Wappsproj-devbin

完成后重启。

5.3 运行结果

点击"File"->“Open”,然后找到 .shp文件打开,下图是效果展示(.shp 文件可以从QGIS官网下载)。

6. 其他问题 6.1 Qt插件未加载

在运行时可能会出现Qt插件未加载的情况。

此时找到QGIS安装目录中的D:SoftwareOSGeo4WappsQt5pluginsplatforms文件夹(根据自己的安装位置进行修改),将其复制到可执行文件(.exe)同一目录下即可。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存