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

karsten vennemann karsten at terragis.net
Tue Apr 5 03:33:29 EDT 2011


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20110405/af8083f9/attachment.html


More information about the Qgis-developer mailing list