Example code: layer.setSubsetString(“‘4′ LIKE “dist_code””’)

# Show every province features (postcodes) one by one

from time import sleep

def stopped(task):
    pass
def completed(exception, result=None):
    pass
def run(task, wait_time):

    # main code
    postcodes = QgsProject.instance().mapLayersByName('postcodes')[0]
    wait_time = wait_time / 100
    prov = [1, 3, 4, 5, 6]
    for i, m in enumerate(prov):
        task.setProgress(i)
        sleep(wait_time)
        postcodes.setSubsetString("'" + str(m) + "'" + ''' LIKE "dist_code"''')

        if task.isCanceled():
            stopped(task)
            return None
    postcodes.setSubsetString("")

task = QgsTask.fromFunction(u'Task', run,
                            on_finished=completed, wait_time=150)
QgsApplication.taskManager().addTask(task)

Or by right click on layer and go to Source and Query Builder:


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: