2013-07-11 45 views

回答

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() 
+0

我忘了提及的是表中每一行的小部件是在按下按鈕時創建的。 – GiannisIordanou

0

「我忘了提的是,當一個按鈕被按下表的每一行中的小部件創建」,在這種情況下,你需要動態地創建他們,你需要能夠識別它們(至少第一個)。

我已經重新實現QComboBox類 - MyComboBox,所以當它們中的任何改變,它將會發射信號firstColumnComboBoxChanged包含標識符(行號),並選擇(「名」或「年齡」)是什麼。該信號將在mainWin類中激活changeSecondCombo方法,其中第二列中的comboBox被更改。

運行此代碼;點擊「添加行」按鈕添加幾行;嘗試在第一列中更改組合框。

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() 
+0

如果它解決了你的問題,請接受答案,如果你需要更多關於代碼的細節,請隨時詢問。 – Aleksandar