I don't know if exists a better method for this question but, I found out this procedure:
How to programmatically close a QMessagebox without clicking ok or X?
where it was programmatically implemented a custom QMessageBox for automatically closing after a certain time. I tried out other methods (involving time python library) with your code but it was necessary a delay equivalent to a QMessagebox for a successfully result.
I slightly modified your code (for adapting to my system) and the proposal in the above link to use a QMessageBox without OK button.
The next code:
class CustomMessageBox(QMessageBox):
def __init__(self, *__args):
QMessageBox.__init__(self)
self.timeout = 0
self.autoclose = False
self.currentTime = 0
def showEvent(self, QShowEvent):
self.currentTime = 0
if self.autoclose:
self.startTimer(1000)
def timerEvent(self, *args, **kwargs):
self.currentTime += 1
if self.currentTime >= self.timeout:
self.done(0)
@staticmethod
def showWithTimeout(timeoutSeconds,
message,
title,
icon = QMessageBox.Information,
buttons = QMessageBox.NoButton):
w = CustomMessageBox()
w.autoclose = True
w.timeout = timeoutSeconds
w.setText(message)
w.setWindowTitle(title)
w.setIcon(icon)
w.setStandardButtons(buttons)
w.exec_()
import processing
from PyQt4.QtGui import QMessageBox
layer = iface.activeLayer();
layer.setSelectedFeatures([])
count =layer.featureCount()
lstChalta = []
lstFeatures = processing.features(layer)
for elem in lstFeatures:
geom = elem.geometry()
attr = elem.attributes()
layer.setSelectedFeatures([elem.id()])
iface.mapCanvas().zoomToSelected( layer )
box = layer.boundingBoxOfSelected()
iface.mapCanvas().setExtent(box)
iface.mapCanvas().refresh()
cno= str(attr[layer.fieldNameIndex('cno')])
path = '/home/zeito/Desktop/folder/Test'+ str(cno) + '.png'
CustomMessageBox.showWithTimeout(.5,
'feature: ' + cno,
"Information",
icon = QMessageBox.Information)
iface.mapCanvas().saveAsImage(path)
it was running with a vector layer where I created your 'cno' field; as it can be observed at the next image:
I took a screenshot during the automatic execution of my code (see next image for feature 3).
After running the code, the target folder had the png images as I expected.