You will need some python code to accomplish this. Have a look at this post which explains, how you add custom business-logic to your widgets.
You will have to save the layerid
and the featureid
which you get when the form is created and use these for your operation.
Please note, that with this code, the layer has to be manually switched into edit mode. Connecting to the appropriate signals from QgsVectorLayer and toggling the button state accordingly is left as an exercise for the reader ;)
QGIS 2.0
from PyQt4.QtCore import *
from PyQt4.QtGui import *
id_flaecheField = None
id_fotoField = None
myDelToolFeature = None
myDelToolLayer = None
def formOpen(dialog,layer,feature):
global myDelToolFeature
global myDelToolLayer
myDelToolFeature = feature
myDelToolLayer = layer
id_fotoField = dialog.findChild(QComboBox,"id_foto")
id_flaecheField = dialog.findChild(QComboBox,"id_flaeche")
buttonDelete= dialog.findChild(QPushButton,"pushButton")
buttonDelete.clicked.connect(delete)
def delete():
# Delete the feature
myDelToolLayer.deleteFeature( myDelToolFeature.id() )
QGIS 1.8
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import QgsMapLayerRegistry
id_flaecheField = None
id_fotoField = None
myDelToolFeatureId = None
myDelToolLayer = None
def formOpen(dialog,layerid,featureid):
global myDelToolFeatureId
global myDelToolLayer
myDelToolFeatureId = featureid
myDelToolLayer = layerid
id_fotoField = dialog.findChild(QComboBox,"id_foto")
id_flaecheField = dialog.findChild(QComboBox,"id_flaeche")
buttonDelete= dialog.findChild(QPushButton,"pushButton")
buttonDelete.clicked.connect(delete)
def delete():
# Get the layer from the registry
layer = QgsMapLayerRegistry.instance().mapLayer( myDelToolLayer )
# Delete the feature
layer.deleteFeature( myDelToolFeatureId )
You can use this structure to loop through the columns of a table.
Dim sCol As String
Dim aCol As Alias
Dim nCol As Integer
For nCol = 1 To TableInfo(sLayer, TAB_INFO_NCOLS)
sCol = ColumnInfo(sLayer, "COL" & nCol, COL_INFO_NAME)
aCol = sLayer & "." & sCol
Next
This will loop through the columns of the table sLayer and store the name in an Alias variable. The next step is to transfer the values from the column to a variable and to be able to write these to an SVG file for other systems to read them back.
To be able to do that in a nice manner you need to convert the data to something that will make sense to this other system.
Here is an example of how that can be done including the loop mentioned above:
Dim sCol As String
Dim aCol As Alias
Dim nCol As Integer
For nCol = 1 To TableInfo(sLayer, TAB_INFO_NCOLS)
sCol = ColumnInfo(sLayer, "COL" & nCol, COL_INFO_NAME)
aCol = sLayer & "." & sCol
sValue = ""
Do Case ColumnInfo(sLayer, sCol, COL_INFO_TYPE)
Case COL_TYPE_CHAR
sValue = aCol
Case COL_TYPE_INTEGER, COL_TYPE_SMALLINT, COL_TYPE_LOGICAL
sValue = Str$(aCol)
Case COL_TYPE_FLOAT, COL_TYPE_DECIMAL
'Converting decimal number to locale formatted numbers
'(Windows Regional Settings)
sValue = FormatNumber$(aCol)
Case COL_TYPE_DATE
If aCol Then
'Converting dates to locale formatted dates
'(Windows Regional Settings)
sValue = FormatDate$(aCol)
End If
Case COL_TYPE_TIME
If aCol Then
'Converting Time to 24 hour clock
sValue = FormatTime$(acol, "HH:mm:ss")
End If
Case COL_TYPE_DATETIME
If aCol Then
'Converting dates to locale formatted dates
'(Windows Regional Settings)
'Converting Time to 24 hour clock
sValue = FormatDate$(aCol) & " " & FormatTime$(GetTime(acol), "HH:mm:ss")
End If
End Case
Next
If you want to add a loop that runs through all the records of the layer too, you will have to wrap the loop above into another Do Until..Loop structure. In this structure, you use the Fetch statements to first fetch the first and then continue to fetch the next record until the cursor is at End Of Table (EOT).
Here's an example of that:
Dim sCol As String
Dim aCol As Alias
Dim nCol As Integer
Fetch First From sLayer
Do Until EOT(sLayer)
For nCol = 1 To TableInfo(sLayer, TAB_INFO_NCOLS)
sCol = ColumnInfo(sLayer, "COL" & nCol, COL_INFO_NAME)
aCol = sLayer & "." & sCol
sValue = ""
Do Case ColumnInfo(sLayer, sCol, COL_INFO_TYPE)
Case COL_TYPE_CHAR
sValue = aCol
Case COL_TYPE_INTEGER, COL_TYPE_SMALLINT, COL_TYPE_LOGICAL
sValue = Str$(aCol)
Case COL_TYPE_FLOAT, COL_TYPE_DECIMAL
'Converting decimal number to locale formatted numbers
'(Windows Regional Settings)
sValue = FormatNumber$(aCol)
Case COL_TYPE_DATE
If aCol Then
'Converting dates to locale formatted dates
'(Windows Regional Settings)
sValue = FormatDate$(aCol)
End If
Case COL_TYPE_TIME
If aCol Then
'Converting Time to 24 hour clock
sValue = FormatTime$(acol, "HH:mm:ss")
End If
Case COL_TYPE_DATETIME
If aCol Then
'Converting dates to locale formatted dates
'(Windows Regional Settings)
'Converting Time to 24 hour clock
sValue = FormatDate$(aCol) & " " & FormatTime$(GetTime(acol), "HH:mm:ss")
End If
End Case
Next
Fetch Next From sLayer
Loop
Hope that helps
Best Answer
This particular way is currently not possible with QGIS (at least not without an advanced plugin)
However, an alternative approach be almost what you want without any python at all and without adding custom buttons.