2

I want to change the data source of a layer (non geometric, just a table of fields) created from a .txt file. I know how to do it manually but I want to do it with the Python console. I have tried the setDataSource() command but I don't quite understand the arguments.

from qgis.core import QgsDataSourceUri

uri = QgsDataSourceUri()
uri.setDataSource("Path_to_new_source_txtfile","layer_name","memory")

This is the code I tried to use to change the source file, it didn't get me any error but it didn't change the source file either.

1 Answer 1

3

You need to call the setDataSource() function from the actual layer, not from the class itself:

layer = iface.activeLayer()
layer.setDataSource("Path_to_new_source_txtfile","layer_name","memory")
0

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.