def appendLine(self, line = None):
"""
Add a line to the table. The new line is inserted before the selected line, or at the end of the table is no line is selected
@param line: a string list containing all the values for this lines. If line is None, an empty line is inserted
"""
selected_row = self.tablewidget.currentRow()
if selected_row < 0 or len(self.tablewidget.selectedIndexes()) <= 0: #Trick to be able to insert lines before the first and after the last line (click on column name to unselect the lines)
selected_row = self.tablewidget.rowCount()
self.tablewidget.insertRow(selected_row)
#If provided, fill the table with the content of the line list
if line is not None and isinstance(line, (list, tuple)) and len(line) >= self.tablewidget.columnCount():
for i in range(self.tablewidget.columnCount()):
#self.tablewidget.setItem(selected_row, i, QTableWidgetItem(line[i]))
self.setCellValue(selected_row, i, line[i])
else:
for i in range(self.tablewidget.columnCount()):
self.setCellValue(selected_row, i, "") #Don't remove this line, otherwise the subclasses won't be able to set custom widget into the table.
#Resize the columns to the content, except for the last one
for i in range(self.tablewidget.columnCount() - 1):
self.tablewidget.resizeColumnToContents(i)
#Resize the rows to the content
for i in range(self.tablewidget.rowCount()):
self.tablewidget.resizeRowToContents(i)
评论列表
文章目录