如何检索已更改的QComboBox的单元格行作为QTableWidget的QCellWidget?

时间:2016-10-11 11:19:23

标签: python pyqt

我试图在QTableWidget中获取更改的组合框的行和列。 这是我的表格设置方式。看看底部,看看我想做什么。

def click_btn_mailouts(self):

    self.cur.execute("""SELECT s.StudentID, s.FullName, m.PreviouslyMailed, m.nextMail, m.learnersDate, m.RestrictedDate, m.DefensiveDate FROM
                        StudentProfile s LEFT JOIN Mailouts m ON s.studentID=m.studentID""")
    self.all_data = self.cur.fetchall()

    self.table.setRowCount(len(self.all_data))
    self.tableFields = ["Check","Full name","Previously mailed?","Next mail","learners date","Restricted date","Defensive driving date"]
    self.columnList = ["StudentID","FullName","PreviouslyMailed","NextMail","learnersDate","RestrictedDate","DefensiveDate"]
    self.table.setColumnCount(len(self.tableFields))
    self.table.setHorizontalHeaderLabels(self.tableFields)
    self.checkbox_list = []
    for i, item in enumerate(self.all_data):
        FullName = QtGui.QTableWidgetItem(str(item[1]))
        PreviouslyMailed = QtGui.QComboBox()
        PreviouslyMailed.addItem("Yes")
        PreviouslyMailed.addItem("No")
        LearnersDate = QtGui.QTableWidgetItem(str(item[3]))
        RestrictedDate = QtGui.QTableWidgetItem(str(item[4]))
        DefensiveDate = QtGui.QTableWidgetItem(str(item[5]))
        NextMail = QtGui.QTableWidgetItem(str(item[6]))
        self.table.setItem(i, 1, FullName)
        self.table.setCellWidget(i, 2, PreviouslyMailed)
        self.table.setItem(i, 3, LearnersDate)
        self.table.setItem(i, 4, RestrictedDate)
        self.table.setItem(i, 5, DefensiveDate)
        self.table.setItem(i, 6, NextMail)
        chkBoxItem = QtGui.QTableWidgetItem()
        chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
        chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
        self.checkbox_list.append(chkBoxItem)
        self.table.setItem(i, 0, self.checkbox_list[i])
        FullName.setFlags(FullName.flags() & ~Qt.ItemIsEditable)
        NextMail.setFlags(NextMail.flags() & ~Qt.ItemIsEditable)

        """Heres what I am trying to do:"""        
        PreviouslyMailed.currentIndexChanged.connect(self.comboBox_change(self.table.cellWidget(row,1).currentText()))
        """I want 'row' to be the row that has the combobox that has been changed."""

1 个答案:

答案 0 :(得分:1)

如果我理解正确,附加信号i将是您要发送的行的值(或者,i+1?)。您可以使用包装函数或lambda轻松地使用Qt信号发送其他数据,如下所示:

for a in range(5):
    x = QComboBox()
    x.currentIndexChanged.connect( lambda i: my_other_function(i, another_var) )

在这里,我们将lambda连接到信号,当它被调用时,内部函数将依次用额外数据调用。这在功能上等同于:

def my_wrapper(i):
    my_other_function(i, another_var)

for a in range(5):
    x = QComboBox()
    x.currentIndexChanged.connect( my_wrapper )

但是,正如您将发现如果您尝试这样做,这并不总是有效。如果您尝试将变量a传递给内部函数,则始终将设置为循环结束时的值a

for a in range(5):
    x = QComboBox()
    x.currentIndexChanged.connect( lambda i: my_other_function(i, a) ) # a will always be 4

你可以通过每次将a的值重新绑定到一个新变量来解决这个问题 - 这是将其作为命名参数传递给lambda的最简单方法。例如 -

for a in range(5):
    x = QComboBox()
    x.currentIndexChanged.connect( lambda i, a=a: my_other_function(i, a) ) # Each combo will send the index, plus the value of a

您应该能够使用上述内容为您创建的每个row发送QComboBox的正确值。