[GIS] Select features and zoom to selection on standalone QGis application

pyqgisstandalonezoom

I'm trying to select and zoom on selected features on standalone QGis application.
Selection seems to work, console shows me list of selected items and they are exactly I expected.

ids = []
for f in kt.getFeatures():
 if f['KatastriNumber']==k_n:
     ids.append(f.id)
     print f['eraldus']
kt.setSelectedFeatures(ids)

But zooming to selection does not work. I have tried many code samples i found from internet. None of them doesnt zoom to selected. Can only zoom to entire layer with:

canvas.setExtent(kt.extent())

I tried to make a box with command:

box = kt.boundingBoxOfSelected()
canvas.setExtent(box)

This gives no result.Then I tried with command

canvas.zoomToSelected(kt)

also no result.
How to tell app that some features from layer are selected?

Best Answer

It required once more canvas refresh to zoom to right place. Final working block:

ids = []
for f in kt.getFeatures():
 if f['KatastriNumber']==k_n:
     ids.append(f.id())
print ids 
kt.setSelectedFeatures(ids)
nt= kt.selectedFeatureCount()
print "Valitud eraldusi: %d"%nt
box = kt.boundingBoxOfSelected()
kt.setSelectedFeatures([])
kt.loadNamedStyle(stiil)
canvas.setExtent(box)
canvas.refresh()
canvas.zoomScale(10000)
canvas.refresh()
canvas.show()