0

我有一个充满组合框和 lineEdits 的 tablewidget,我想找到一种方法来让在组合框中选择的选项影响 tableWidget 中另一个组合框中的可用选项。有什么想法吗?

4

2 回答 2

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()
于 2013-09-09T08:11:38.050 回答
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()
于 2013-07-11T17:38:56.737 回答