Qgis (2) Add a zoom-in and roaming Toolbar

Source: Internet
Author: User
Tags qt designer

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.




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.