After adding a layer in qgis, you can scroll and zoom with the mouse. If you want to add an icon to the toolbar to achieve the same effect,
(1) Add the following variables to qmainwindow:
Qtoolbar * mpmaptoolbar; // <q qqqqqq* mppantool; // <roaming qgsmaptool * mpzoomintool; // <enlarge qgsmaptool * mpzoomouttool; /// <zoom out qgsmaptool * mpzoomfull; // <full Graph Display
Add action in the QT designer:
(2) then add the following in the initialization function:
// Add the button mpmaptoolbar = addtoolbar (TR ("Tools") on the toolbar; mpmaptoolbar-> addseparator (); mpmaptoolbar-> addaction (UI. mpactionpan); mpmaptoolbar-> addaction (UI. mpactionzoomin); mpmaptoolbar-> addaction (UI. mpactionzoomout); mpmaptoolbar-> addaction (UI. mpactionzoomfull); mppantool = new qgsmaptoolpan (mainmapcanvas); mppantool-> setaction (UI. mpactionpan); mpzoomintool = new qgsmaptoolzoom (mainmapcanvas, false); mpzoomintool-> setaction (UI. mpactionzoomin); mpzoomouttool = new qgsmaptoolzoom (mainmapcanvas, true); mpzoomouttool-> setaction (UI. mpactionzoomout );
(3) Add a function connection to the signal slot:
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.mpActionZoomFull, SIGNAL(triggered()), this, SLOT(zoomFull()));
(4) Implementation of Slot functions:
void MainWindow::zoomInMode(){mainMapCanvas->setMapTool(mpZoomInTool);if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer){return;}QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);pLayer->removeSelection(true);}void MainWindow::zoomOutMode(){mainMapCanvas->setMapTool(mpZoomOutTool);if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer){return;}QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);pLayer->removeSelection(true);}void MainWindow::panMode(){mainMapCanvas->setMapTool(mpPanTool);ui.mpActionPan->setCheckable(true);ui.mpActionPan->setChecked(true);if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer){return;}QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);pLayer->removeSelection(true);}void MainWindow::zoomFull(){mainMapCanvas->zoomToFullExtent();if ( mainMapCanvas->layer(0)->type() == QgsMapLayer::RasterLayer){return;}QgsVectorLayer *pLayer=(QgsVectorLayer *)mainMapCanvas->layer(0);pLayer->removeSelection(true);}
The mainmapcanvas above has already loaded the vector layer.