Document directory
- Step 1: Modify the qgis2.h file and add the following header files
- Step 2: Modify the qgis2.cpp file to modify the addlayer function. The modified code is as follows:
- Step 3: Modify the main function to support Chinese display. The Code is as follows:
- Step 4: run the command as follows:
This article mainly implements the function of loading raster data. The main class used is qgsrasterlayer, and the code is modified based on the second lecture. Step 1: Modify the qgis2.h file and add the following header files
// 4th code # include <qgsrasterlayer. h> # include <qmessagebox. h>
Step 2: Modify the qgis2.cpp file to modify the addlayer function. The modified code is as follows:
// 4th code qfileinfo myrasterfileinfo (".. /data/abarema_jupunba_projection.tif "); qgsrasterlayer * myplayer = new qgsrasterlayer (myrasterfileinfo. filepath (), myrasterfileinfo. completebasename (); If (myplayer-> isvalid () {qmessagebox: Information (null, qstring (TR ("load layer ")), qstring (TR ("loaded successfully"), qmessagebox: Yes | qmessagebox: No); myplayer-> setdrawingstyle (qgsrasterlayer: singlebandpseudo docolor); myplayer-> Publish (qgsrasterlayer:: pseudo docolorshader); myplayer-> constraint (qgscontrastenhancement: Unknown, false); qlist <qgsmapcanvaslayer> mylayerset; qgsmaplayerregistry: instance ()-> addmaplayer (myplayer, true ); mpmapcanvas-> setextent (myplayer-> extent (); mylayerset. append (qgsmapcanvaslayer (myplayer); mpmapcanvas-> setlayerset (mylayerset);} else {qmessagebox: Information (null, qstring (TR ("load layer ")), qstring (TR ("loading failed"), qmessagebox: Yes | qmessagebox: No );}
Note: (1) differences between basename and completebasename: The character returned by basename does not contain ".", while the character returned by completebasename does not contain the character after the last.
Step 3: Modify the main function to support Chinese display. The Code is as follows:
QgsApplication a(argc, argv,TRUE);QTextCodec *codec = QTextCodec::codecForName("System");QTextCodec::setCodecForCStrings(codec);QTextCodec::setCodecForLocale(codec);QTextCodec::setCodecForTr(codec);qgis2 w;w.show();return a.exec()
Step 4: run the command as follows: