3

Using 3.10.1-A Coruña on Windows 10

I am writing a Python Script in the QGIS Editor but iface.mapCanvas().zoomToSelected() does not appear to be working at all in the Editor.

iface.mapCanvas().zoomToSelected() - works fine in the Python Console

Editor code - apparently does nothing

selectid = [2]
poalayer.select(selectid)
iface.mapCanvas().zoomToSelected() # does nothing
iface.mapCanvas().refresh();

Console - works as expected

iface.mapCanvas().zoomToSelected()

Let me know if I can provide any further info or do a test.

Added additional info: There's something obviously wrong here. From this reply (QGIS: Zoom to selected feature via plugin doesn't works in first attempt) I tried running my script a second time in the same session and it worked.

1 Answer 1

2

I tried in the console, in the editor and in a plugin and everything works very well.

The code in the console:

iface.mapCanvas().zoomToSelected()

The code in the editor:

layer = iface.activeLayer()
selectid = [2]
layer.select(selectid)
iface.mapCanvas().zoomToSelected()

The code in the plugin:

# At the beginning 
import qgis.utils
from qgis.utils import iface

# In run() function. I added a pushbutton.
self.dlg.pushButton.clicked.connect(self.zoom_canvas)
self.dlg.show()

# The function when I press on the push button (after run() function)
def zoom_canvas(self):
    iface.mapCanvas().zoomToSelected()
1
  • Thank you Vincent Bré. I don't fully understand why your version works and mine did not. I did shut down the computer overnight so maybe I needed to reboot QGIS. I'll try rebooting QGIS if I have these sorts of problems in the future. Commented Dec 27, 2019 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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