第一步:新建Qt Application 项目,名称为:qgis2,完成后,其解决方案的文件如下所示
第二步,设计UI
(1)双击,qgis2.ui文件,弹出qtDesigner设计器,添加相应菜单(对应的Action命名)如下图所示
(2)在面板中央的centralWidget中拖入一个QFrame容器(用来包含qgsmapcanvas),命名为frameMap;
(3)在qtDesigner中,点击保存,退出。
(4)在项目解决方案中,有点点击qgis2.ui,点击编译,使其重新生成ui_qgis2.h这个文件
第三步:修改qgis2.h文件
(PS:在Generated Files中有个ui_qgis2.h文件,此文件是有qgis2.ui文件自动生成的,无需修改,如需用到此类,用法当如:Ui::qgis2Class,如下代码所示)
修改的qigs2.h文件如下:
- #ifndef QGIS2_H
- #define QGIS2_H
- #include <QtGui/QMainWindow>
- #include "ui_qgis2.h"
- #include <qgsapplication.h>
- #include <qgsproviderregistry.h>
- #include <qgssinglesymbolrenderer.h>
- #include <qgsmapcanvas.h>
- #include <qgsvectorlayer.h>
- #include <qgsmaptool.h>
- #include <QtGui/QVBoxLayout>
- #include <qgsmaplayerregistry.h>
- #include <QtGui/QToolBar>
- #include<qgsmaptoolpan.h>
- #include <qgsmaptoolzoom.h>
- class qgis2 : public QMainWindow,private Ui::qgis2Class
- {
- Q_OBJECT
- public:
- qgis2(QWidget *parent = 0, Qt::WFlags flags = 0);
- ~qgis2();
- public slots:
- void zoomInMode();
- void zoomOutMode();
- void panMode();
- void addLayer();
- private:
- Ui::qgis2Class ui;
- QgsMapCanvas *mpMapCanvas;
- QVBoxLayout *mpMapLayout;
- QToolBar * mpMapToolBar;
- QgsMapTool *mpPanTool;
- QgsMapTool *mpZoomInTool;
- QgsMapTool *mpZoomOutTool;
- };
- #endif // QGIS2_H
第四步:修改qgis2.cpp文件
- #include "qgis2.h"
- qgis2::qgis2(QWidget *parent, Qt::WFlags flags)
- : QMainWindow(parent, flags)
- {
- ui.setupUi(this);
- QString myPluginsDir = "c:/Program Files/qgis1.7.0/plugins";
- QgsProviderRegistry::instance(myPluginsDir);
- //创建地图画布
- mpMapCanvas= new QgsMapCanvas(0,0);
- mpMapCanvas->freeze(false);
- mpMapCanvas->enableAntiAliasing(true);
- mpMapCanvas->setCanvasColor(QColor(255,255,255));
- mpMapCanvas->useImageToRender(false);
- mpMapCanvas->setVisible(true);
- mpMapCanvas->refresh();
- mpMapCanvas->show();
- mpMapCanvas->setFocus();
- //将窗口部件布局
- mpMapLayout = new QVBoxLayout();
- mpMapLayout->addWidget(mpMapCanvas);
- ui.frameMap->setLayout(mpMapLayout);
- setCentralWidget(ui.frameMap);
- //创建Action行为
- connect(ui.mpActionPan,SIGNAL(triggered()),this,SLOT(panMode()));
- connect(ui.mpActionZoomIn,SIGNAL(triggered()),this,SLOT(zoomInMode()));
- connect(ui.mpActionZoomOut,SIGNAL(triggered()),this,SLOT(zoomOutMode()));
- connect(ui.mpActionAddLayer,SIGNAL(triggered()),this,SLOT(addLayer()));
- //创建工具条功能
- mpMapToolBar=addToolBar(tr("File"));
- mpMapToolBar->addAction(ui.mpActionAddLayer);
- mpMapToolBar->addAction(ui.mpActionPan);
- mpMapToolBar->addAction(ui.mpActionZoomIn);
- mpMapToolBar->addAction(ui.mpActionZoomOut);
- //创建maptool功能
- mpPanTool= new QgsMapToolPan(mpMapCanvas);
- mpPanTool->setAction(ui.mpActionPan);
- mpZoomInTool = new QgsMapToolZoom(mpMapCanvas,FALSE);
- mpZoomInTool->setAction(ui.mpActionZoomIn);
- mpZoomOutTool = new QgsMapToolZoom(mpMapCanvas,TRUE);
- mpZoomOutTool->setAction(ui.mpActionZoomOut);
- }
- qgis2::~qgis2()
- {
- delete mpZoomOutTool;
- delete mpZoomInTool;
- delete mpPanTool;
- delete mpMapToolBar;
- delete mpMapCanvas;
- delete mpMapLayout;
- }
- void qgis2::panMode()
- {
- mpMapCanvas->setMapTool(mpPanTool);
- }
- void qgis2::zoomInMode()
- {
- mpMapCanvas->setMapTool(mpZoomInTool);
- }
- void qgis2::zoomOutMode()
- {
- mpMapCanvas->setMapTool(mpZoomOutTool);
- }
- void qgis2::addLayer()
- {
- //读取矢量数据
- QString myLayerPath ="E:\\Qgis\\project\\qgis2\\data";// 此处换成自己的矢量文件
- QString myLayerBaseName = "test";
- QString myPoviderName="ogr";
- QList<QgsMapCanvasLayer> myLayerSet;
- QgsVectorLayer* myLayer = new QgsVectorLayer(myLayerPath,myLayerBaseName,myPoviderName);
- if (myLayer->isValid())
- {
- QgsSingleSymbolRenderer *myRenderer = new QgsSingleSymbolRenderer(myLayer->geometryType());
- myLayer->setRenderer(myRenderer);
- //增加矢量数据到图层注册
- QgsMapLayerRegistry::instance()->addMapLayer(myLayer,true);
- //设置画布的extent
- mpMapCanvas->setExtent(myLayer->extent());
- //设置画布的图层数据集
- myLayerSet.append(QgsMapCanvasLayer(myLayer));
- mpMapCanvas->setLayerSet(myLayerSet);
- }
- else
- {
- return;
- }
- }
第五步:修改main.cpp文件
- #include "qgis2.h"
- #include <QtGui/QApplication>
- int main(int argc, char *argv[])
- {
- QgsApplication a(argc, argv,TRUE);
- qgis2 w;
- w.show();
- return a.exec();
- }