2016-09-15 57 views
0

我想添加一個checkbow到QTableWidget中的每一行,不幸的是它似乎只出現在第一行。這裏是我的代碼:Python - 添加複選框到QTableWidget的每一行

data = ['first_row', 'second_row', 'third_row'] 
nb_row = len(data) 
nb_col = 2 

qTable = self.dockwidget.tableWidget 
qTable.setRowCount(nb_row) 
qTable.setColumnCount(nb_col) 
chkBoxItem = QTableWidgetItem() 

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

我是否缺少明顯的東西?

我查了下帖子也:

回答

2

好吧,我不得不把chkBoxItem = QTableWidgetItem()最後一個循環內(我猜它創建一個新的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)