Does anybody know where i can find implementation of feature called "Zoom to Feature".

To use this function from GUI you have to right click on layer then choose "Open Attribute Table" then right click on feature and there should be option called "Zoom to Feature" in context menu.

It looks like this:

enter image description here


Feature in Qgis is a set of attributes from table.

enter image description here

  • Witch context menu are you referring to ? Also if you are not selecting a feature in the table to witch feature do you want to zoom ? – J.R Oct 17 '19 at 13:24
  • Are you wanting the QgsMapCanvas::zoomToSelected() method? – Joseph Oct 17 '19 at 13:42
  • @Joseph Nope i dont want this method because it assumes that feature was previously selected. I dont want to select any feature. I just want to zoom my map view to some feature on some layer. – BannanaFunTime Oct 17 '19 at 13:45
  • @J.R Im reffering to context menu presented in the attached picture. There is a feature selected in the attached picture. As far as i know Feature in Qgis is set of attributes in table. – BannanaFunTime Oct 17 '19 at 13:48

It is what i was looking 4.

    void QgsIdentifyResultsDialog::zoomToFeature()
  QTreeWidgetItem *item = lstResults->currentItem();

  QgsVectorLayer *vlayer = vectorLayer( item );
  QgsRasterLayer *rlayer = rasterLayer( item );
  if ( !vlayer && !rlayer )

  QgsMapLayer *layer = nullptr;
  if ( vlayer )
    layer = vlayer;
    layer = rlayer;

  QgsIdentifyResultsFeatureItem *featItem = dynamic_cast<QgsIdentifyResultsFeatureItem *>( featureItem( item ) );
  if ( !featItem )

  QgsFeature feat = featItem->feature();
  if ( !feat.hasGeometry() )

  // TODO: verify CRS for raster WMS features
  QgsRectangle rect = mCanvas->mapSettings().layerExtentToOutputExtent( layer, feat.geometry().boundingBox() );

  if ( rect.isEmpty() )
    QgsPointXY c = rect.center();
    rect = mCanvas->extent();
    rect.scale( 0.5, &c );

  mCanvas->setExtent( rect );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.