Using 3.10.1-A Coruña on Windows 10
I need to automate the production of thousands of images in QGIS3. I've tried the many sleep and QTimer solutions offered in many questions on this site but that is not working for me. It appears that QGIS pauses once with the sleep and QTimer and then executes the rest of the whole script in one go. Consequently the images produced with the Sleep and QTimer methods are all the same.
I can pause the script with a QMessageBox between each zoomToSelected as shown below, and I get the correct images, but that means I'll be clicking the message box thousands of times for 1 dataset.
This example is not optimized with a function but is laid out so I can see what is happening. I'm quite new to QGIS3 scripting.
import time
from qgis.PyQt.QtCore import QTimer
def doNothing(): # pausing kludge
print ("doing nothing")
# MAKE IMAGES
pcode = '0800'
layer = iface.activeLayer()
layer.removeSelection()
layer.selectByExpression('"POA_CODE16"='+pcode)
iface.mapCanvas().resize(QSize(1200,1000))
iface.mapCanvas().zoomToSelected()
iface.mapCanvas().refresh()
QMessageBox.information(None, "DEBUG:", pcode)
#time.sleep(1)
#QTimer.singleShot(3000, doNothing)
iface.mapCanvas().saveAsImage("D:/test/file_" + pcode + ".png")
pcode = '0810'
layer = iface.activeLayer()
layer.removeSelection()
layer.selectByExpression('"POA_CODE16"='+pcode)
iface.mapCanvas().zoomToSelected()
iface.mapCanvas().refresh()
QMessageBox.information(None, "DEBUG:", pcode)
#time.sleep(1)
#QTimer.singleShot(3000, doNothing)
iface.mapCanvas().saveAsImage("D:/test/file_" + pcode + ".png")
pcode = '0822'
layer = iface.activeLayer()
layer.removeSelection()
#selectid = [4]
#layer.select(selectid)
layer.selectByExpression('"POA_CODE16"='+pcode)
iface.mapCanvas().zoomToSelected()
iface.mapCanvas().refresh()
QMessageBox.information(None, "DEBUG:", pcode)
#time.sleep(5)
#QTimer.singleShot(3000, doNothing)
iface.mapCanvas().saveAsImage("D:/test/file_" + pcode + ".png")
Requested additional info for PolyGeo:
I have about 100 links in my Google Chrome History for visits to gis.stackexchange yesterday. I was initially trying to get zoomToSelected to work (in a loop) - that problem was solved with help from a forum user - now I'm looking at the saveAsImage in the loop as well. So the pages I visited and tested were about controlling loops - either for saveAsImage or zoomToSelected.
Some links from my research/ tests yesterday:
How to refresh MapCanvas while PyQGIS-script is running?
Select feature one by one not working in pyQGIS?
mapCanvas.saveAsImage() doesn't work properly within loop in standalone script (only .PGW)