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: