Skip to content Skip to sidebar Skip to footer

Python - Add Checkbox To Every Row In QTableWidget

I am trying to add a checkbow to every row in a QTableWidget, unfortunately it only seems to appear in the first row. Here is my code: data = ['first_row', 'second_row', 'third_row

Solution 1:

Ok, I just had to put chkBoxItem = QTableWidgetItem() inside the last loop (I guess it has to create a new QTableWidgetItem() item for each row...):

for col in [1]:
    chkBoxItem = QTableWidgetItem()
    chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
    chkBoxItem.setCheckState(QtCore.Qt.Unchecked)       
    qTable.setItem(row,col,chkBoxItem)

Solution 2:

self.ui.tableWidget.setColumnCount(4)
self.ui.tableWidget.setRowCount(4)
self.ui.tableWidget.verticalHeader().setVisible(False)
self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

alaaa = ['Source Image', 'Dimension', 'Format', "File size"]
self.ui.tableWidget.setHorizontalHeaderLabels(alaaa)

for row, string in enumerate(alaaa, 0):
    chkBoxItem = QTableWidgetItem(string)
    chkBoxItem.setText(string)
    chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
    chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
    self.ui.tableWidget.setItem(row, 0, chkBoxItem)

enter image description here


Post a Comment for "Python - Add Checkbox To Every Row In QTableWidget"