[PyKDE] Adding qsqlquerymodel to a qcombobox

joanne matthews (RRes-Roth) joanne.matthews at bbsrc.ac.uk
Thu Sep 21 15:45:11 BST 2006


Here's my problem,

I've got a qtablewidget with a column that contains a qcombobox in every
row. What I'd like to be able to do is to set the rows in the qcombobox
to the records returned from a the qsqlquerymodel so that the combo box
displays 3 columns for every row. I don't have any problems adding
single items to the combobox using:
Combobox.additem(model.record(row).value(col).toString() but I don't
know how to add all 3 columns as a single item.
I may be going about this in totally the wrong way so any alternative
implementations would be appreciated. I have implemented a rather
inelegant solution by concatenating strings until I can come up with a
better idea and have included a cut down version of my program to give a
better idea of what I'm trying to achieve:

class myClass(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        
        self.table=self.createTable(72,3)
        mainLayout = QtGui.QVBoxLayout()
        
        
        tableLayout=QtGui.QHBoxLayout()
        tableLayout.addWidget(self.table)
        mainLayout.addLayout(tableLayout)
        self.setLayout(mainLayout)
    
    def createTable(self,row,col):
        #creates a table widget    
        
        table = QtGui.QTableWidget(row, col)
        for row in range(table.rowCount()):
            table.setCellWidget(row,2,self.createComboBox(table))
            
        return table
    
    def createComboBox(self,table):
        self.comboBox=QtGui.QComboBox(table)
        self.createModel()
        return self.comboBox
    
    def createModel(self):
        model=QtSql.QSqlQueryModel()
        model.setQuery("select col1,col2,col3 from tablename")
        
        for row in range(model.rowCount()):
            record=""
            for col in range(model.columnCount()):
                record+=model.record(row).value(col).toString()+" | "
            self.comboBox.addItem(record)




More information about the PyQt mailing list