Qgis secondary development Lecture 3

Source: Internet
Author: User
Document directory
  • Step 1: add the relevant header file in the qgis2.h file. The three main headers are as follows: qgslabel. H, qgslabelattribute. h qgsfield. h.
  • Step 2: In the qgis2.cpp file, add the code to the addlayer () function. The main code is as follows:
The third section mainly implements the label function of vector elements. The Code is the first step to be modified on the basis of section 2: add relevant header files in the qgis2.h file. The main three header files are as follows: qgslabel. h, qgslabelattribute. h qgsfield. h file

The Code is as follows:

# 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> # include <qgsvectordataprovider. h> // mark the added header file # include <qgslabel. h> // a class used to render the label. The label rendering attribute can be specified directly or dynamically specified based on the attribute value # include <qgslabelattributes. h> // This class stores the attribute values required for label rendering, such as the word size and position. # include <qgsfield. h> // This class is used to wrap fields into attribute tables or data sources. // Add the header file of the QT dialog box # include <qmessagebox. h> # include <qstring. h>
Step 2: In the qgis2.cpp file, add the code to the addlayer () function. The main code is as follows:

Void qgis2: addlayer () {// read Vector Data qstring mylayerpath = ".. /Data "; qstring mylayerbasename =" test "; qstring mypovidername =" OGR "; qlist <qgsmapcanvaslayer> mylayerset; optional * mylayer = new inline (mylayerpath, mylayerbasename, mypovidername ); if (mylayer-> isvalid () {callback * myrenderer = new qgssinglesymbolrenderer (mylayer-> geometrytype (); mylayer-> setrenderer (myrenderer); qgslabel * myplabel; // obtain the label pointer myplabel = mylayer-> label (); qgslabelattributes * myplabelattributes; myplabelattributes = myplabel-> layerattributes (); // obtain the Field List of the vector layer // typedef qmap <int, qgsfield> qgsfieldmapqgsfieldmap myfields = mylayer-> dataprovider ()-> fields (); For (unsigned int I = 0; I <myfields. size (); I ++) {qmessagebox: Information (null, qstring (TR ("field information"), qstring (myfields [I]. name ()). tolocal8bit (), qmessagebox: Yes);} // use the last field as the marked field myplabel-> setlabelfield (qgslabel: Text, myfields. size ()-1); // set the label color. You need to use the labelattribute object myplabelattributes-> setcolor (QT: Black); myplabelattributes-> setbufferenabled (true ); myplabelattributes-> setbuffercolor (QT: yellow); int mytype = qgslabelattributes: pointunits; myplabelattributes-> setbuffersize (1, mytype ); // enable annotation mylayer-> enablelabels (true); // Add vector data to layer registration 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 3: Modify the main. cpp file to support Chinese display. The specific code is as follows:

Int main (INT argc, char * argv []) {qgsapplication A (argc, argv, true); // set qtextcodec * codec = qtextcodec :: codecforname ("system"); qtextcodec: setcodecforlocale (codec); qtextcodec: setcodecforcstrings (codec); qtextcodec: setcodecfortr (codec); qgis2 W; W. show (); Return a.exe C ();}

The final program execution result is shown in:

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.