首页 > 代码库 > QGIS

QGIS

project(GisFreeMap)

set(CMAKE_BUILD_TYPE Debug)
find_package(Qt4 REQUIRED QtCore QtGui QtXml)
include(${QT_USE_FILE})

include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/core")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/core/symbology-ng")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/core/geometry")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/core/layertree")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/gui")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/gui/editorwidgets/core")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/gui/layertree")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0/src/gui/attributetable")
include_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0-build")
include_directories("E:/BaiduYunDownload/qgis/3rdparty/include")

link_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0-build/src/gui/Debug")
link_directories("E:/BaiduYunDownload/qgis/qgis-2.18.0-build/src/core/Debug")

add_definitions(-DCORE_EXPORT=)
add_definitions(-DGUI_EXPORT=)
add_definitions(-D_USE_MATH_DEFINES)
add_definitions(-Dnoexcept=)
set(
    SRC 
    main.cpp    
)


add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} qgis_gui qgis_core ${QT_LIBRARIES})

 

#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrendererv2.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsmapcanvas.h>
#include <qgsproject.h>
#include <qgslayertreenode.h>
#include <qgslayertreegroup.h>
#include <qgslayertreeregistrybridge.h
#include <qgsattributetablemodel.h>
#include <qgsattributetableview.h>
#include <qgsattributetablefiltermodel.h>
#include <qgsvectorlayercache.h>
#include <qgseditorwidgetregistry.h>
#include <qgspallabeling.h>
#include <qgscustomlayerorderwidget.h>
int main(int argc, char ** argv)
{
    QgsApplication app(argc, argv, true);
    QgsApplication::setPluginPath("E:/BaiduYunDownload/qgis/qgis-2.18.0-build/output/plugins/Debug");
    QgsApplication::initQgis();

    QgsMapCanvas * canvas = new QgsMapCanvas();
//    QgsProject::instance()->read(QFileInfo("D:/1.qgs"));
    QgsLayerTreeMapCanvasBridge * bridge = new QgsLayerTreeMapCanvasBridge(QgsProject::instance()->layerTreeRoot(), canvas);
    QgsVectorLayer * vectorLayer = new QgsVectorLayer("D:/shpFile/china/point_sheng.shp", "point_sheng", "ogr");
    QgsVectorLayer * vectorLayer1 = new QgsVectorLayer("D:/shpFile/china/china.shp", "china", "ogr");
    QgsVectorLayer * vectorLayer2 = new QgsVectorLayer("D:/shpFile/World/line_world.shp", "line_world", "ogr");

    QgsPalLayerSettings layerSettings;
    layerSettings.enabled = true;

    // 设置显示字段
    layerSettings.fieldName = vectorLayer->pendingFields()[0].name(); 
    layerSettings.centroidWhole = true; // 设置位置参考的中心点

    // Label 字体设置
    layerSettings.textColor = QColor( 0, 0, 0 ); // 设置字体颜色
    layerSettings.textFont = QFont( "msyh", 12 ); // 设置字体和大小

    // Label 轮廓buffer设置
    layerSettings.bufferDraw = true;
    layerSettings.bufferColor = QColor( 255, 0, 0 ); // 轮廓buffer的颜色
    layerSettings.bufferSize = 1; // 轮廓buffer大小
    layerSettings.bufferTransp = 0.5; // 轮廓buffer的透明度

    // Label 阴影绘制
    layerSettings.shadowDraw = true;
    layerSettings.shadowOffsetAngle = 135; // 阴影的角度
    layerSettings.shadowOffsetDist = 1; // 阴影与Label的距离



    layerSettings.setDataDefinedProperty( layerSettings.Size, true, false, NULL, "size" ); // 设置 Label 大小
    layerSettings.setDataDefinedProperty( layerSettings.Color, true, false, NULL, "color" ); // 设置 Label 颜色
    layerSettings.setDataDefinedProperty( layerSettings.Family, true, false, NULL, "font" ); // 设置 Label 字体


    layerSettings.writeToLayer( vectorLayer ); // 将配置写入图层

    QgsMapLayerRegistry::instance()->addMapLayer(vectorLayer);
    QgsMapLayerRegistry::instance()->addMapLayer(vectorLayer1);
    QgsMapLayerRegistry::instance()->addMapLayer(vectorLayer2);
    canvas->setAutoFillBackground(true);
    canvas->setBackgroundBrush(QBrush(Qt::black));
    canvas->show();
    QgsEditorWidgetRegistry::initEditors( canvas ); // 一定要做这步,其实最好是main函数一开始就执行这句

    QgsVectorLayerCache* lc = new QgsVectorLayerCache( vectorLayer, vectorLayer->featureCount() );

    QgsAttributeTableView* tv = new QgsAttributeTableView();
    QgsAttributeTableModel* tm = new QgsAttributeTableModel( lc );
    tm->loadLayer(); // 一定不要忘记,否则model里面没有图层的属性数据

    QgsAttributeTableFilterModel* tfm = new QgsAttributeTableFilterModel( canvas, tm, tm );
    tfm->setFilterMode( QgsAttributeTableFilterModel::ShowAll );
    tv->setModel( tfm );

    tv->show();
    QgsCustomLayerOrderWidget * w = new QgsCustomLayerOrderWidget(bridge);
    w->show();

   
    QgsProject::instance()->write(QFileInfo("D:/2.qgs"));

    return app.exec();
}

技术分享

 

QGIS