效果
Vs2017项目注意事项
1、环境搭建查看:QGis二次开发 —— 1、Windows10搭建Vs2017-QGis环境(附Vs2017环境效果)(附:Qt助手加入QGis接口说明文档)
2、在Vs项目中Qt模块加入“xml”,防止出错。
源代码示例1:加载文件测试
#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
#include <QMessageBox>
#include <QtMath>
#include <qgsrasterlayer.h>
void Test_QGIS()
{
QString fileName = "./world.tif";
QgsRasterLayer *my_rasterLayer = new QgsRasterLayer(fileName, "fileName", "gdal");
if (!my_rasterLayer || !my_rasterLayer->isValid())
{
QMessageBox::warning(0, "qgis", "load tif fail...", QMessageBox::Yes);
}
else
{
QMessageBox::warning(0, "qgis", "load tif success!!!", QMessageBox::Ok);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test_QGIS();
QtWidgetsApplication1 w;
w.show();
return a.exec();
}
源代码示例2:加载并显示tif栅格文件
注意: 在加载tif栅格文件后会在该文件同级目录下自动创建.html文件。
main.cpp
#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
#include <qgsapplication.h>
int main(int argc, char *argv[])
{
// 参数三: 如果需要GUI应用程序,则设置为true;对于仅控制台应用程序,设置false
QgsApplication a(argc, argv, true);
QgsApplication::setPrefixPath("D:/Software/QGis-OSGeo4W/install/apps/qgis-ltr", true); // 设置qgis路径
QgsApplication::setPluginPath("D:/Software/QGis-OSGeo4W/install/apps/qgis-ltr/plugins"); // 设置插件路径
QgsApplication::initQgis(); // 初始化QGis
QtWidgetsApplication1 w;
w.show();
return a.exec();
}
QtWidgetsApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication1.h"
#include <QtMath>
#include <qgsmapcanvas.h>
class QtWidgetsApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication1(QWidget *parent = nullptr);
~QtWidgetsApplication1();
protected:
void initQgsMapCanvas();
bool loadRasterTif(const QString &FileName);
private:
Ui::QtWidgetsApplication1Class ui;
QgsMapCanvas *qMapCanvas = nullptr;
};
QtWidgetsApplication1.cpp
#include "QtWidgetsApplication1.h"
#include <QGridLayout>
#include <qgsrasterlayer.h>
#include <qgsproject.h>
QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
initQgsMapCanvas();
loadRasterTif("./world.tif");
}
QtWidgetsApplication1::~QtWidgetsApplication1()
{
QLayout *GridLayout = ui.qgsMapCanvasWidget->layout();
if (GridLayout)
{
GridLayout->removeWidget(qMapCanvas);
delete GridLayout;
}GridLayout = nullptr;
if (qMapCanvas) { delete qMapCanvas; }qMapCanvas = nullptr;
}
void QtWidgetsApplication1::initQgsMapCanvas()
{
// 创建地图画布对象
qMapCanvas = new QgsMapCanvas(this);
QGridLayout *GridLayout = new QGridLayout;
GridLayout->addWidget(qMapCanvas);
ui.qgsMapCanvasWidget->setLayout(GridLayout);
}
bool QtWidgetsApplication1::loadRasterTif(const QString &FileName)
{
// 创建光栅层对象并加入本地文件
QgsRasterLayer *qRasterLayer = new QgsRasterLayer(FileName, "raster");
if (!qRasterLayer || !qRasterLayer->isValid()) { return false; }
// 设置对比度增强(主要用于栅格图像;矢量图像不需要)。参数1算法:线性直方图、参数2界限范围:实际最小最大值
qRasterLayer->setContrastEnhancement(QgsContrastEnhancement::StretchToMinimumMaximum, QgsRasterMinMaxOrigin::MinMax);
// 画布设置
qMapCanvas->setLayers(QList<QgsMapLayer*>() << qRasterLayer); // 设置应在画布中显示的图层列表
qMapCanvas->setDestinationCrs(qRasterLayer->crs()); // 设置目标坐标参照系为输入光栅图层的坐标系
qMapCanvas->setExtent(qRasterLayer->extent()); // 设置画布范围为输入光栅图层的范围
qMapCanvas->setCurrentLayer(qRasterLayer); // 设置画布当前图层为输入光栅图层
qMapCanvas->refresh(); // 刷新画布
// 将图层列表添加到已加载图层的映射中
QgsProject::instance()->addMapLayers(QList<QgsMapLayer*>() << qRasterLayer);
return true;
}
QtWidgetsApplication1.ui
源代码示例2,最终运行结果
源代码示例3:加载并显示.shp矢量文件
注意: .shp文件目录下应该要有对应同名的.shx矢量字体文件,这样才能加载成功。
main.cpp
#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
#include <qgsapplication.h>
int main(int argc, char *argv[])
{
// 参数三: 如果需要GUI应用程序,则设置为true;对于仅控制台应用程序,设置false
QgsApplication a(argc, argv, true);
QgsApplication::setPrefixPath("D:/Software/QGis-OSGeo4W/install/apps/qgis-ltr", true); // 设置qgis路径
QgsApplication::setPluginPath("D:/Software/QGis-OSGeo4W/install/apps/qgis-ltr/plugins"); // 设置插件路径
QgsApplication::initQgis(); // 初始化QGis
QtWidgetsApplication1 w;
w.show();
return a.exec();
}
QtWidgetsApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication1.h"
#include <QtMath>
#include <qgsmapcanvas.h>
class QtWidgetsApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtWidgetsApplication1(QWidget *parent = nullptr);
~QtWidgetsApplication1();
protected:
void initQgsMapCanvas();
bool loadRasterTif(const QString &FileName);
bool loadVectorShp(const QString &FileName);
private:
Ui::QtWidgetsApplication1Class ui;
QgsMapCanvas *qMapCanvas = nullptr;
};
QtWidgetsApplication1.cpp
#include "QtWidgetsApplication1.h"
#include <QGridLayout>
#include <qgsrasterlayer.h>
#include <qgsvectorlayer.h>
#include <qgsproject.h>
QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent) : QMainWindow(parent)
{
ui.setupUi(this);
initQgsMapCanvas();
//loadRasterTif("./world.tif");
loadVectorShp("./china.shp");
}
QtWidgetsApplication1::~QtWidgetsApplication1()
{
QLayout *GridLayout = ui.qgsMapCanvasWidget->layout();
if (GridLayout)
{
GridLayout->removeWidget(qMapCanvas);
delete GridLayout;
}GridLayout = nullptr;
if (qMapCanvas) { delete qMapCanvas; }qMapCanvas = nullptr;
}
void QtWidgetsApplication1::initQgsMapCanvas()
{
// 创建地图画布对象
qMapCanvas = new QgsMapCanvas(this);
QGridLayout *GridLayout = new QGridLayout;
GridLayout->addWidget(qMapCanvas);
ui.qgsMapCanvasWidget->setLayout(GridLayout);
}
bool QtWidgetsApplication1::loadRasterTif(const QString &FileName)
{
// 创建光栅层对象并加入本地文件
QgsRasterLayer *qRasterLayer = new QgsRasterLayer(FileName, "raster");
if (!qRasterLayer || !qRasterLayer->isValid()) { return false; }
// 设置对比度增强(主要用于栅格图像;矢量图像不需要)。参数1算法:线性直方图、参数2界限范围:实际最小最大值
qRasterLayer->setContrastEnhancement(QgsContrastEnhancement::StretchToMinimumMaximum, QgsRasterMinMaxOrigin::MinMax);
// 画布设置
qMapCanvas->setLayers(QList<QgsMapLayer*>() << qRasterLayer); // 设置应在画布中显示的图层列表
qMapCanvas->setDestinationCrs(qRasterLayer->crs()); // 设置目标坐标参照系为输入光栅图层的坐标系
qMapCanvas->setExtent(qRasterLayer->extent()); // 设置画布范围为输入光栅图层的范围
qMapCanvas->setCurrentLayer(qRasterLayer); // 设置画布当前图层为输入光栅图层
qMapCanvas->refresh(); // 刷新画布
// 将图层列表添加到已加载图层的映射中
QgsProject::instance()->addMapLayers(QList<QgsMapLayer*>() << qRasterLayer);
return true;
}
bool QtWidgetsApplication1::loadVectorShp(const QString &FileName)
{
// 创建光栅层对象并加入本地文件
QgsVectorLayer *qVectorLayer = new QgsVectorLayer(FileName, "shp");
bool aaa =qVectorLayer->isValid();
if (!qVectorLayer || !qVectorLayer->isValid()) { return false; }
// 画布设置
qMapCanvas->setLayers(QList<QgsMapLayer*>() << qVectorLayer); // 设置应在画布中显示的图层列表
qMapCanvas->setDestinationCrs(qVectorLayer->crs()); // 设置目标坐标参照系为输入光栅图层的坐标系
qMapCanvas->setExtent(qVectorLayer->extent()); // 设置画布范围为输入光栅图层的范围
qMapCanvas->setCurrentLayer(qVectorLayer); // 设置画布当前图层为输入光栅图层
qMapCanvas->refresh(); // 刷新画布
// 将图层列表添加到已加载图层的映射中
QgsProject::instance()->addMapLayers(QList<QgsMapLayer*>() << qVectorLayer);
return true;
}
QtWidgetsApplication1.ui
源代码示例3,最终运行结果
关注
笔者 - jxd