Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

While using QGIS and having loaded a non spatial table (from PostGIS), is it possible to add a new row to its table attributes without having to open the attribute table first as in here Is it possible to add a new line in a non-spatial table using Qgis?

Same principle as add a point but with no geometry, just information in a table.

I was able to create a plugin that chooses and starts editing the table but can't get the form window to open so I can fill the fields.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I found a way to do it. Here is the code i used

# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from qgis.gui import *

import resources_rc

class SelectFeaturesPlugin:

def __init__(self, iface):
    # Save reference to the QGIS interface
    self.iface = iface

def initGui(self):

    # Create action that will start plugin configuration
    self.action5 = QAction(QIcon(":/plugins/selectfeatures/icon.png"), u"Add Record", self.iface.mainWindow())
    # connect the action to the run method
    QObject.connect(self.action5, SIGNAL("triggered()"), self.addRecord)

    # Add toolbar button and menu item
    self.iface.addToolBarIcon(self.action5)
    self.iface.addPluginToMenu(u"&Add Record", self.action5)

def unload(self):
    # Remove the plugin menu item and icon
    self.iface.removeToolBarIcon(self.action5)    
    self.iface.removePluginMenu(u"&Add Record",self.action5)

# run method that performs all the real work
def addRecord(self):
    feat = QgsFeature()
    layer = self.iface.activeLayer()
    layer.startEditing()
    layer.addFeature( feat )

    from myattrdialog import AttributeDialog
    dlg = AttributeDialog( layer )
    if not dlg.exec_():
      return

    #Populate the feature attrs map usin the values the user inserted in the dialog
    feat.setAttributeMap( dlg.getFeatureAttributeMap() )
    layer.commitChanges()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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