PyQt : Checkbox in QTableWidget

前端 未结 1 547
误落风尘
误落风尘 2020-12-09 22:46

I use following code to put a checkbox in the 9th column of my QTableWidget

chkBoxItem = QtGui.QTableWidgetItem()
chkBoxItem.setFlags(QtCore.Qt.         


        
相关标签:
1条回答
  • 2020-12-09 23:48

    One way to do it would be:

    1. connect the itemClicked signal of the table to a handler

    2. test the checkedState of the clicked item in the handler

    3. if the item is checked, add its row to the list

    Example code:

    from PyQt4 import QtGui, QtCore
    
    class Window(QtGui.QWidget):
        def __init__(self, rows, columns):
            QtGui.QWidget.__init__(self)
            self.table = QtGui.QTableWidget(rows, columns, self)
            for column in range(columns):
                for row in range(rows):
                    item = QtGui.QTableWidgetItem('Text%d' % row)
                    if row % 2:
                        item.setFlags(QtCore.Qt.ItemIsUserCheckable |
                                      QtCore.Qt.ItemIsEnabled)
                        item.setCheckState(QtCore.Qt.Unchecked)
                    self.table.setItem(row, column, item)
            self.table.itemClicked.connect(self.handleItemClicked)
            layout = QtGui.QVBoxLayout(self)
            layout.addWidget(self.table)
            self._list = []
    
        def handleItemClicked(self, item):
            if item.checkState() == QtCore.Qt.Checked:
                print('"%s" Checked' % item.text())
                self._list.append(item.row())
                print(self._list)
            else:
                print('"%s" Clicked' % item.text())
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window(6, 3)
        window.resize(350, 300)
        window.show()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题