3

I am trying to build plugin that allow to quick update attribute table of selected features. User select feature from vector layer then select values from plugin, click apply and table is updated. I got four combo box from which you can select values and click button "Apply changes" that should execute code to change attribute table.

My code below:

def run(self):
        """Run method that loads and starts the plugin"""

        self.dlg.connect(self.dlg.apply,SIGNAL("clicked()"),
                         self.execute_changes) #SIGNAL is not defined

        print str(result)


        if not self.pluginIsActive:
            self.pluginIsActive = True

            #print "** STARTING NM_quick_updator"

            # dockwidget may not exist if:
            #    first run of plugin
            #    removed on close (see self.onClosePlugin method)
            if self.dockwidget == None:
                # Create the dockwidget (after translation) and keep reference
                self.dockwidget = quick_updateDockWidget()

            # connect to provide cleanup on closing of dockwidget
            self.dockwidget.closingPlugin.connect(self.onClosePlugin)

            # show the dockwidget
            # TODO: fix to allow choice of dock location
            self.iface.addDockWidget(Qt.TopDockWidgetArea, self.dockwidget)
            self.dockwidget.show()

    def execute_changes(self):

            #Execute changing attributes when button is clicked
            layer = self.iface.activeLayer()
            selected_features = layer.selectedFeatures()

            fields = layer.pendingFields()   

            field_names = [field.name() for field in fields]

            print field_names

            #selectedLayerIndex = self.dlg.currentIndex()
            #selectedLayer = layers[selectedLayerIndex]
            #selFeatures = selectedLayer.selectedFeatures()
            selFeatures =selected_features
            selectedLayer = layer
            ids = [f.id() for f in selFeatures]
            print ids

            selectedLayer.startEditing()

            #here i take values from combo boxes and use them to update columns
            for fid in ids:
                print fid

                selectedLayer.changeAttributeValue(fid, 6, int(self.dlg.Name_1.currentText()))
                selectedLayer.changeAttributeValue(fid, 2, str(self.dlg.Name_2.currentText()))

            print "completed changing values"
            selectedLayer.commitChanges()
            print "commiting changes"
            mc =  self.iface.mapCanvas()
            mc.refresh()
            print "refresh map"

I can access to my combo box inside changeAttribiuteValues but I would like to update table with given name not using FID number. I created list of my shapefile columns but I don't know how to use them with combo boxes.

Also I don't know how to run my code when push button is clicked. Now on when I load my plugin I got error: 'global name 'SIGNAL' is not defined'

Can someone help me?

I am using QGIS 2.16.

3

I manage to find an answer. This code actually do what i ask for.

def run(self):
   """Run method that loads and starts the plugin"""


   if not self.pluginIsActive:
       self.pluginIsActive = True


       if self.dockwidget == None:
           # Create the dockwidget (after translation) and keep reference
           self.dockwidget = quick_updateDockWidget()


       self.dockwidget.closingPlugin.connect(self.onClosePlugin)

       # show the dockwidget
       # TODO: fix to allow choice of dock location
       self.iface.addDockWidget(Qt.TopDockWidgetArea, self.dockwidget)
       self.dockwidget.show()
       self.dockwidget.apply.clicked.connect(self.execute_changes)

def execute_changes(self):

   print "executing changes"

   #take selected features from active layer

   layer = self.iface.activeLayer()
   selected_features = layer.selectedFeatures()

   #take feature fields names

   fields = layer.pendingFields()   

   field_names = [field.name() for field in fields]

   print field_names

   selFeatures =selected_features
   selectedLayer = layer
   ids = [f.id() for f in selFeatures]
   print ids

   selectedLayer.startEditing()

   #here i take values from combo boxes and use them to update columns
   for fid in ids:
       print fid

       selectedLayer.changeAttributeValue(fid, field_names.index("NAME_1"), str(self.dockwidget.VEGSPAN.currentText()))
       selectedLayer.changeAttributeValue(fid, field_names.index("NAME_2"), str(self.dockwidget.PCONFIG.currentText()))

   print "completed changing values"
   selectedLayer.commitChanges()
   print "commiting changes"
   mc =  self.iface.mapCanvas()
   mc.refresh()
   print "refresh map"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.