简体   繁体   中英

To schedule a repeating event in qgis using Python

I have to schedule a job in qgis every n seconds. In the meantime i will be able to do other stuff (for example visualize the attributes of an object). I have implemented a code like this in Python:

import time,threading...

interval=60
def job():
   ....
   ....
   ....
   threading.Timer(interval,job).start()
threading.Timer(interval,job).start() 

When I launch the script it remains suspended and does not do anything

I put here the entire code for completeness:

import time,threading
import re,glob,os
from PyQt4.QtGui import QColor

interval=5
def job():
    lay=qgis.utils.iface.activeLayer()
    iterator=range(50)
    counter=0
    for i in iterator:
        if lay<>None and not(re.search("com",lay.name())):
           QgsMapLayerRegistry.instance().removeMapLayer(lay.id())
           lay=qgis.utils.iface.activeLayer()
    dir="/home_local/titan/projDir/data/titan/shapefiles/shapefile/"
    lista=os.listdir(dir)
    exp="shp"
    for file in lista: 
        if re.search(exp,file):
           counter=counter+1           
           lay=qgis.utils.iface.addVectorLayer(dir+file,file+str(counter),"ogr") 
           symbols = lay.rendererV2().symbols()
           symbol = symbols[0]
           if re.search("F30",file):
               symbol.setColor(QColor.fromRgb(50,50,250))
           else :
               symbol.setColor(QColor.fromRgb(150,200,200))
           qgis.utils.iface.mapCanvas().refresh() 
           qgis.utils.iface.legendInterface().refreshLayerSymbology(lay)
           lay.setLayerTransparency(30)
    threading.Timer(interval,job).start()
threading.Timer(interval,job).start()

NB. without the threading the job works.

Try using a QTimer instead. You can connect its timeout signal to a slot to do your processing (in a Python thread if necessary).

from PyQt4.QtCore import QTimer
timer = QTimer()
timer.timeout.connect(my_slot)
timer.start(1000) # start 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM