0

The canvas is not showing the changes after 1 second. The point is visible only after the whole script gets executed.

Here is the Script:

import time

def refresh():
    cachingEnabled = iface.mapCanvas().isCachingEnabled()
    for layer in iface.mapCanvas().layers():
        if cachingEnabled:
            layer.triggerRepaint()
    iface.mapCanvas().refresh() 

def spin(seconds):
    time_end = time.time() + seconds
    while time.time() < time_end:
        pass

canvas=iface.mapCanvas()
m = QgsVertexMarker(canvas)
m.setCenter(QgsPointXY(269614.47214304556837305,2099557.2543975617736578))
m.setColor(QColor(255,0,255))
m.setFillColor(QColor(255,255,0))
m.setIconSize(10)
m.setIconType(QgsVertexMarker.ICON_CIRCLE)
for i in range(5):
        m.hide()
        refresh()
        spin(1)
        m.show()
        refresh()
        spin(1)

2 Answers 2

1

Maybe you should use "flashFeatureIds" from "QgsMapCanvas" which is designed for that.

layer = iface.activeLayer()
features = layer.getFeatures()
feature = QgsFeature()
features.nextFeature(feature)
iface.mapCanvas().flashFeatureIds(layer, [feature.id()])

You would need to transform your QgsVertexMarker to a QgsVectorLayer before, or maybe there is another way to flash without vector layer.

2
  • I think this will flash for limited time. Actually my long term goal is to animate a taxi along some points in a path (Showing the point at one point then moving on next point and hiding the previous point). I think this will not work for that.
    – Abhinivesh
    Commented Aug 15, 2021 at 14:36
  • So you should describe your full goal. Have a look to "refresh layer" in vector properties and then "display". You can set an interval for refresh. And use a geometry generator to make the feature changing opacity along the time. youtube.com/watch?v=2jsYRxmnWkI
    – etrimaille
    Commented Aug 17, 2021 at 10:41
0

Is there a reason why you are using

range(5):

Does this not suggest that it will accumulate the refresh 5 times? Therefore adding a refresh of 5 seconds, as opposed to 1? (actually, 10 seconds, because you spin() twice in the loop).

Try changing the range() to 1 and only putting calling spin() once.

3
  • I used range because I want to create a point blinking type of animation. And I am refreshing the canvas after the spin function. But its not refreshing , that is the main problem.
    – Abhinivesh
    Commented Aug 11, 2021 at 8:15
  • yes i have a funny feeling that because the refresh() calls are inside the for loop, that they aren't excecuting untilt he loop is exited. Test without the for loop to start with.
    – nr_aus
    Commented Aug 11, 2021 at 8:25
  • 1
    I have no idea, but I guess, this might never work. The Python inside QGIS might not be threaded, so the "refresh" is just sitting in a queue or something similar waiting for the end of the script. I already tried to make a feature blinking each after the other one with one second interval, it didn't work a long time ago.
    – etrimaille
    Commented Aug 11, 2021 at 9:32

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.