Aug-12-2019, 07:12 PM
Hello everyone,
I'm writing a QGIS tool and I think I got it but it doesn't work. Maybe someone can help me.
In the Qgs Project is a .csv table file with two fields and two features/Objects.
The script I'm writing is supposed to replace two textlabels in my open layout with the attributes in the second field of the table.
My script has the following procedure:
1) take the table from the project and store it in a variable
2) take the text labels from the layout and store it in a variable
3) compare the text labels with the attributes from the first field and if they are equal overwrite the text label with the content from the second field.
My script doesn't give me an error message but it is not doing anything. apparently I missed something but I don't see what it is. Hoping for help.
Here is the code:
I guess there is something wrong in the changeLabel function but I don't see what it is. Or I completely forgot something. Perhaps my question is too specific for this forum but I thought maybe someone can help.
I'm writing a QGIS tool and I think I got it but it doesn't work. Maybe someone can help me.
In the Qgs Project is a .csv table file with two fields and two features/Objects.
The script I'm writing is supposed to replace two textlabels in my open layout with the attributes in the second field of the table.
My script has the following procedure:
1) take the table from the project and store it in a variable
2) take the text labels from the layout and store it in a variable
3) compare the text labels with the attributes from the first field and if they are equal overwrite the text label with the content from the second field.
My script doesn't give me an error message but it is not doing anything. apparently I missed something but I don't see what it is. Hoping for help.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
def getTable( self ): """gets table from the QGIS Project""" layer = None for lyr in QgsProject.instance().mapLayers().values(): if lyr.name() = = "table" : layer = lyr break return layer def setVariable( self ): """adds the table to a variable called self.inTable""" self .inTable = self .getTable() def changeLabel( self ): """saves the Layout label texts in a variable""" labels = None for l in QgsProject.instance().layoutManager().layouts()[ 0 ].items(): if isinstance (l,QgsLayoutItemLabel): labels = l """gets the features of the table""" features = self .inTable.getFeatures() """compares the list of labels with the attributes in the first column of the features and replaces the label with the attribute from the second column""" for label in labels: for feat in features: if label = = feat.attributes()[ 0 ]: labels[label] = feat.attributes()[ 1 ] return labels def run( self ): """Run method that performs all the real work""" self .dlg.show() # Run the dialog event loop result = self .dlg.exec_() # See if OK was pressed if result: self .setVariable() self .changeLabel() |