[Qgis-developer] add form elements dynamically - qgis python plugin

Bernhard Ströbl Bernhard.Stroebl at jena.de
Tue Apr 5 04:22:29 EDT 2011


Hi Karsten,

I think you need to add the QComboBox to your dialog in order to make it 
show
_but_ if you have a ui file why don't you create the QComboBox in 
QtDesigner?
you can access it like this (ui file must have been converted into py 
file with pyuic4):
comboBoxNew = self.ui.comboBoxNew

I was in a similar situation (beginner with python, Qt and QGIS) some 8 
months ago, so here are some other thoughts:

be sure that you have read about QGIS' dynamic forms
http://linfiniti.com/2009/11/creating-dynamic-forms-a-new-feature-headed-for-qgis-1-4/

some Python code to use them:
http://www.osgeo.org/pipermail/qgis-developer/2010-January/008731.html

last but not least:
You can define a comboBox in the layer properties (edit widget = value 
map) and populate it from a csv file. The values are saved with the 
project or a qml file. It is even possible to manipulate the qml file 
thus synchronising the values with the current version of the csv file 
and then reapply it to the layer. I could provide some code for this if 
you are interested.

regards

Bernhard

karsten vennemann schrieb:
> Hi I am a beginner with python , QGIS and pyQT.
>  
> I am working on a python plug-in and would like to populate dynamically 
> a pyQT based QDialog form with combo boxes at runtime (the elements to 
> be added are input via a config txt file that is read). Thus I would 
> like to get hints how I can do this .
>  
> I have a a py file with the dialog form (streameditcustomformdialog.py):
>  
> from PyQt4 import QtCore, QtGui
> from ui_streameditcustomform import Ui_StreamEditCustomForm
> # create the dialog for zoom to point
> class StreamEditCustomFormDialog(QtGui.QDialog):
>     def __init__(self, parent = None):
>         QtGui.QDialog.__init__(self)
>         # Set up the user interface from Designer.
>         self.ui = Ui_StreamEditCustomForm()
>         self.ui.setupUi(self)
>  
> and a routine that reads the config file and should populate the form 
> with new comboxes:
>  
> def inputConfig(self):
>        
>         # QgisMainWindow = qgis.utils.iface.mainWindow()       
>         filename = QFileDialog.getOpenFileName(None, 'Open File', "C:/", \
>             "Text Files (*.txt *.csv)");              
>         if filename:
>             formconfig_input = open(filename, 'r')   # input species 
> info id , name for batch processing
>             while 1:
>                 formconfig_line  = formconfig_input.readline()         # 
> read  csv text file line
>                 if not formconfig_line: break
>                 formconfig_splitline = formconfig_line.split(',')
>                 if len(formconfig_splitline) < 3:
>                     QMessageBox.information(None,"A problem occured with 
> your input file.", \
>                     "The file you choose did not have the proper \n 
> syntax or number of comma separated fields.")
>                     return
>                 else:
>                     fieldname1   = formconfig_splitline[0]
>                     fieldtype1   = formconfig_splitline[1]
>                     fieldlength1 = formconfig_splitline[2]
>                     QMessageBox.information(None,"Form Info:", \
>                     fieldname1 + '\n' + fieldtype1 + '\n' + fieldlength1)
>                     self.dlg = StreamEditCustomFormDialog()
>                     #show the dialog
>                     self.dlg.comboBoxNew = QtGui.QComboBox()
>                     self.dlg.comboBoxNew.setObjectName("comboBox")
>                     self.dlg.comboBoxNew.setItemText(1, 
> QtGui.QApplication.translate("Form", "bw", None, 
> QtGui.QApplication.UnicodeUTF8))
>                     self.dlg.show()
>                     return
>             formconfig_input.close
>  
> However I tried it above - the combo box does not appear on the form 
> which is showing ok if I run the plugin, no error message ...but no 
> combo box ...
>  
> What am I doing wrong?
> Will this take care of layout management or how could I have the layout 
> (e.g. size of form handled if many combo boxes are added ?
>  
> Thanks
> Karsten
> 
> Karsten Vennemann
> 
> Terra GIS LTD
> www.terragis.net <http://www.terragis.net>
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
> 
> ________ Information from NOD32 ________
> This message was checked by NOD32 Antivirus System for Linux Mail Server.
> http://www.nod32.com

-- 
Dipl.-Geogr. Bernhard Ströbl
Anwendungsbetreuer GIS
Kommunale Immobilien Jena
  - Datenverarbeitung -
mailto:Bernhard.Stroebl at jena.de
http://kij.jena.de/
----------------------------


________ 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