如何在PyQT中的qtableWidget中连接两个小部件?

时间:2013-07-11 07:51:15

标签: python user-interface pyqt

我有一个充满了comboBoxes和lineEdits的tablewidget,我想找到一种方法来获取在comboBox中选择的选项会影响tableWidget.Any想法中另一个comboBox中的可用选项吗?

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

“我忘了提到的是,按下按钮时会创建表格每行中的小部件”,在这种情况下,您需要动态创建它们,并且您需要能够识别它们(至少是第一)。

我已经重新实现了QComboBox类 - MyComboBox,所以当它们中的任何一个被更改时,它将发出包含标识符(行号)和所选内容的信号firstColumnComboBoxChanged( “名字”或“年龄”)。该信号将激活changeSecondCombo类中的mainWin方法,其中第二列中的comboBox已更改。

运行此代码;单击“添加行”按钮添加几行;尝试在第一列中更改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()