[Qgis-developer] Plugin help

Bernhard.Stroebl at jena.de Bernhard.Stroebl at jena.de
Tue Jun 7 09:37:18 EDT 2011


Hi Michael,

here is some sample code to get the idea. The code lives in a QtDialog 
subclass; the dialog is designed using QtDesigner:

#I use a QTableWidget defined in QtDesigner with the name radweg (cyle 
path in English)
self.tblRadweg = self.ui.radweg

# I create a QSqlQuery to get all cycle paths related from my db
query = QtSql.QSqlQuery()
query.prepare(<SELECT string>)
query.exec_()
while query.next():
# returns false when all records are done
     self.tblRadwegAppendRow(query.value(0), \
     query.value(1).toString(), \
     query.value(2).toString(), \
     query.value(3).toString())

     # function to fill a row in the QTableWidget
     def tblRadwegFillRow(self, thisRow, radwegId, \
         artname, richtung, lage):

         radwegItem = QtGui.QTableWidgetItem(artname)
         radwegItem.radwegId = radwegId # the PK is attached here
         self.tblRadweg.setItem(thisRow, 0, radwegItem)
         self.tblRadweg.setItem(thisRow, 1, \
            QtGui.QTableWidgetItem(richtung))
         self.tblRadweg.setItem(thisRow, 2, QtGui.QTableWidgetItem(lage))

     # function to add  a row in the QTableWidget
     def tblRadwegAppendRow(self, radwegId, artname, richtung, lage):
         thisRow = self.tblRadweg.rowCount()
         # identical with index of row to be appended as
         # row indices are 0 based
         self.tblRadweg.setRowCount(thisRow + 1) # append a row
         self.tblRadwegFillRow(thisRow, radwegId, artname, \
             richtung, lage)

     # Slot when user double clicks on a row
     @QtCore.pyqtSlot()
     def on_radweg_itemDoubleClicked(self, radwegItem):
         thisRow = self.tblRadweg.currentRow()
         radwegItem = self.tblRadweg.item(thisRow, 0)
         radwegId = radwegItem.radwegId
         self.showRadweg(radwegId, thisRow)

     def showRadweg(self, radwegId, thisRow):
	# function to open another dialog where user can
         # input data for the cycle path


     # I have two buttons on the dialog one with "+" named "add"
     #the other with "-" named "remove"
     # here are their slots

     @QtCore.pyqtSlot()
     def on_btnAdd_clicked(self):
         self.showRadweg(-9999, -9999)
         # this opens the input form where the appropriate
         # INSERT statement is created
         # if user clicks OK there use self.tblRadwegAppendRow
         # to show the newly created dataset in your table

     @QtCore.pyqtSlot()
     def on_btnRemove_clicked(self):
         thisRow = self.tblRadweg.currentRow()
         radwegItem = self.tblRadweg.item(thisRow, 0)
         radwegId = radwegItem.radwegId
         # QtSqlQuery to delete the dataset
         query = QtSql.QSqlQuery(self.db)
         statement = QtCore.QString("DELETE FROM <radweg_table> \
         " WHERE id = :radwegId;")
         query.prepare(statement)
         query.bindValue(":radwegId", QtCore.QVariant(radwegId))
         query.exec_()

         if query.isActive():
             query.finish()
             # remove the row from the QTableWidget
             self.tblRadweg.removeRow(thisRow)
         else:
             self.showQueryError(query) # my error handler
             query.finish()

Two remarks:
1) More elegant IMHO would be a solution with a QTableModel and a QTableView
2) With QGIS' ability to load geometryless tables from a DB the hole 
thing could be done with QgsFeatures, too. Depending on how many records 
you have the DB solution with SQL will be faster, though.

hope this helps

Bernhard


Am 07.06.2011 14:41, schrieb Michael Crider:
> I am fairly new to QGIS and to Python, although I have been in
> programming and GIS for several years. I need to write or customize a
> plugin that will act as an editor for a layer, with the addition of a
> table at the bottom showing related records from a join table that has a
> one-to-many relationship. There needs to be a way to edit and delete
> these records, as well as add new ones. Does anyone know of an existing
> plugin that does something similar to this, or have some sample code
> they would be willing to share to get me started?
>




________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com


More information about the Qgis-developer mailing list