Pyside QTablewidget qcheckbox点击事件

时间:2018-05-24 06:52:04

标签: python pyqt pyside qtablewidget qcheckbox

我已经在qtablewidget的第一个单元格中添加了qcheckbox现在我想要的是如何访问复选框以及如何获取其click事件。基本上我想要的是复选框检查行应该被选中并且应该取消选中未选中的行。 这是代码:

def add_items(self):
    rows = 5
    cols = 5
    self.tableWidget.setRowCount(5)
    self.tableWidget.setColumnCount(5)
    for r in range(0, rows):
        for c in range(1, cols):

            self.qwidget = QtGui.QWidget()
            self.checkbox = QtGui.QCheckBox()
            self.checkbox.setCheckState(QtCore.Qt.Checked)
            qhboxlayout = QtGui.QHBoxLayout(self.qwidget)
            qhboxlayout.addWidget(self.checkbox)
            qhboxlayout.setAlignment(QtCore.Qt.AlignCenter)
            qhboxlayout.setContentsMargins(0, 0, 0, 0)
            self.tableWidget.setCellWidget(r, 0, self.qwidget)

            item = QTableWidgetItem('note'+str(r))
            self.tableWidget.setItem(r, c, item)
    print 'done'

1 个答案:

答案 0 :(得分:0)

您可以使用itemChanged信号。

from PySide.QtCore import Slot, Qt
from PySide.QtGui import QTableWidgetItem

@Slot(QTableWidgetItem)
def on_tableWidget_itemChanged(self, item):
    """ Handles the row's state
    :type item: QTableWidgetItem
    :parameter item: The changed item
    """
    checked = item.checkState() == Qt.Checked
    if checked:  # the item gets checked
        # do stuff here..
    else:  # the item gets unchecked
        # do stuff here