Second Lecture on qgis Development

Source: Internet
Author: User
Document directory
  • Step 1: Create a QT application project named qgis2. The solution file is as follows:
  • Step 2: design the UI
  • Step 3: Modify the qgis2.h File
  • Step 4: Modify the qgis2.cpp File
  • Step 5: Modify the main. cpp File
  • Step 6: The running result is as follows:
Step 1: Create a QT application project named qgis2. The solution file is as follows:

Step 2: design the UI

(1) double-click the qgis2.ui file to bring up the qtdesigner and add the corresponding menu (corresponding action name), as shown in

(2) drag a qframe container (used to include qgsmapcanvas) into the centralwidget in the center of the panel and name it framemap;

(3) In qtdesigner, click Save to exit.

(4) In the project solution, click "qgis2.ui" and click "compile" to regenerate the file "ui_qgis2.h ".

Step 3: Modify the qgis2.h File

(PS: There is a ui_qgis2.h file in generated files. This file is automatically generated by the qgis2.ui file and does not need to be modified. If this type is required, use it as UI: qgis2class, as shown in the following code)

The modified qigs2.h file is as follows:

#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_OBJECTpublic: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

Step 4: Modify the qgis2.cpp File

# 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 ); // create a map canvas mpmapcanvas = new qgsmapcanvas (255,255,255); mpmapcanvas-> freeze (false); mpmapcanvas-> enableantialiasing (true); mpmapcanvas-> setcanvascolor (qcolor )); mpmapcanvas-> useimagetorender (False); mpmapcanvas-> setvisible (true); mpmapcanvas-> refresh (); mpmapcanvas-> show (); mpmapcanvas-> setfocus (); // layout the widget mpmaplayout = new qvboxlayout (); mpmaplayout-> addwidget (mpmapcanvas); UI. framemap-> setlayout (mpmaplayout); setcentralwidget (UI. framemap); // create action Action connect (UI. mpactionpan, signal (triggered (), this, slot (panmode (); Connect (UI. mpactionzoomin, signal (triggered (), this, slot (zoominmode (); Conn ECT (UI. mpactionzoomout, signal (triggered (), this, slot (zoomoutmode (); Connect (UI. mpactionaddlayer, signal (triggered (), this, slot (addlayer (); // create the toolbar function mpmaptoolbar = addtoolbar (TR ("file ")); mpmaptoolbar-> addaction (UI. mpactionaddlayer); mpmaptoolbar-> addaction (UI. mpactionpan); mpmaptoolbar-> addaction (UI. mpactionzoomin); mpmaptoolbar-> addaction (UI. mpactionzoomout); // create maptool function mppantool = new qgsmaptoolpan (M Pmapcanvas); 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 partition; 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 (volume);} void qgis2 :: addlayer () {// read Vector Data qstring mylayerpath = "e :\\ qgis \ project \ qgis2 \ data "; // change the Vector file qstring mylayerbasename = "test"; qstring mypovidername = "OGR"; qlist <qgsmapcanvaslayer> mylayerset; optional * mylayer = new struct (mylayerpath, mylayerbasename, mypovidername); If (mylayer-> isvalid () {qgssinglesymbolrenderer * myrenderer = new ender( mylayer-> geometrytype (); mylayer-> setrenderer (myrenderer ); // Add vector data to the layer to register qgsmaplayerregistry: instance ()-> addmaplayer (mylayer, true ); // set extentmpmapcanvas-> setextent (mylayer-> extent () of the canvas; // set the layer dataset mylayerset of the canvas. append (qgsmapcanvaslayer (mylayer); mpmapcanvas-> setlayerset (mylayerset);} else {return ;}}

Step 5: Modify the main. cpp File

#include "qgis2.h"#include <QtGui/QApplication>int main(int argc, char *argv[]){QgsApplication a(argc, argv,TRUE);qgis2 w;w.show();return a.exec();}

Step 6: The running result is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.