0

I have a tablewidget full of comboBoxes and lineEdits and I want to find a way to get the choice selected in a comboBox affect the available choices in another comboBox in the tableWidget.Any ideas?

IordanouGiannis
  • 4,149
  • 16
  • 65
  • 99

2 Answers2

0
   import sys
   from PyQt4 import QtGui, QtCore

   class mainWin(QtGui.QWidget):
       def __init__(self, parent=None):
           QtGui.QWidget.__init__(self, parent)
           self.di = {"name":["raton", "kamal", "sujon"], "age":["45","21","78"]}

           lay = QtGui.QGridLayout(self)
           self.combo1=QtGui.QComboBox()
           self.combo2=QtGui.QComboBox()
           lay.addWidget(self.combo1, 0, 0)
           lay.addWidget(self.combo2, 0, 1)

           self.combo1.addItems(["name", "age"])

           self.combo2.addItems(self.di["name"])

           self.connect(self.combo1, QtCore.SIGNAL("currentIndexChanged (const QString&)"),   
            self.changeCombo)



      def changeCombo(self, ind):
          self.combo2.clear()
          self.combo2.addItems(self.di[ind])





 def main():
    app = QtGui.QApplication(sys.argv)
    win = mainWin()
    win.show()
    sys.exit(app.exec_())

 main()
raton
  • 418
  • 5
  • 14
0

"What I forgot to mention is that the widgets in each row of the table are created when a button is pushed", in that case you need to create them dynamically and you need to be able to identify them (at least the first one).

I've re-implemented QComboBox class - MyComboBox, so when any of them is changed it will emit signal firstColumnComboBoxChanged that contains identifier (row number) and what is selected ("name" or "age"). That signal will activate changeSecondCombo method in mainWin class, where comboBox in second column is changed.

Run this code; click button "Add row" to add few rows; try to change comboBox in first column.

import sys
from PyQt4 import QtGui, QtCore

class myComboBox(QtGui.QComboBox):
    def __init__(self, comboID, mainForm):
        super(myComboBox, self).__init__()
        self.__comboID = comboID
        self.__mainForm = mainForm

        self.connect(self, QtCore.SIGNAL("currentIndexChanged (const QString&)"), self.indexChanged)

    def indexChanged(self, ind):
        # send signal to MainForm class, self.__comboID is actually row number, ind is what is selected
        self.__mainForm.emit(QtCore.SIGNAL("firstColumnComboBoxChanged(PyQt_PyObject,PyQt_PyObject)"), self.__comboID, ind) 

class mainWin(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.di = {"name":["raton", "kamal", "sujon"], "age":["45","21","78"]}

        lay = QtGui.QGridLayout(self)
        #create tableWidget and pushButton
        self.tableWidget = QtGui.QTableWidget()
        self.tableWidget.setColumnCount(2)
        self.pushButton = QtGui.QPushButton()
        self.pushButton.setText("Add row")
        lay.addWidget(self.tableWidget, 0, 0)
        lay.addWidget(self.pushButton, 1, 0)

        self.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.addRow)

        # Custom signal
        self.connect(self, QtCore.SIGNAL("firstColumnComboBoxChanged(PyQt_PyObject, PyQt_PyObject)"),         self.changeSecondCombo)

    def addRow(self):
        rowNumber = self.tableWidget.rowCount()
        self.tableWidget.insertRow(rowNumber)

        combo1=myComboBox(rowNumber, self)
        combo2=QtGui.QComboBox()
        combo1.addItems(["name", "age"])
        combo2.addItems(self.di["name"])

        self.tableWidget.setCellWidget(rowNumber, 0, combo1)
        self.tableWidget.setCellWidget(rowNumber, 1, combo2)


    def changeSecondCombo(self, row, ind):
        combo2 = self.tableWidget.cellWidget(row, 1)
        if combo2:
            combo2.clear()
            combo2.addItems(self.di["%s"%(ind)])

def main():
    app = QtGui.QApplication(sys.argv)
    form = mainWin()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()
Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • Please accept the answer if it solves your problem, also, if you need more details about the code, feel free to ask. – Aleksandar Sep 23 '13 at 12:40