2024-10-01 04:53:44 发布
网友
我有一个充满了组合框和行编辑的tablewidget,我想找到一种方法,让在组合框中选择的选项影响另一个组合框中的可用选项tableWidget。任何思想?在
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()
“我忘了提的是,表中每一行的小部件都是在按下按钮时创建的”,在这种情况下,您需要动态地创建它们,并且您需要能够识别它们(至少是第一个)。在
我已经重新实现了QComboBox类-MyComboBox,因此当其中任何一个被更改时,它将发出包含标识符(行号)和所选内容(“name”或“age”)的信号firstColumnComboBoxChanged。该信号将激活changeSecondCombo类中的changeSecondCombo方法,其中第二列中的comboBox被更改。在
QComboBox
MyComboBox
firstColumnComboBoxChanged
changeSecondCombo
点击“在第一行添加代码”按钮。在
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()
“我忘了提的是,表中每一行的小部件都是在按下按钮时创建的”,在这种情况下,您需要动态地创建它们,并且您需要能够识别它们(至少是第一个)。在
我已经重新实现了
QComboBox
类-MyComboBox
,因此当其中任何一个被更改时,它将发出包含标识符(行号)和所选内容(“name”或“age”)的信号firstColumnComboBoxChanged
。该信号将激活changeSecondCombo
类中的changeSecondCombo
方法,其中第二列中的comboBox被更改。在点击“在第一行添加代码”按钮。在
相关问题 更多 >
编程相关推荐