Issue
I have a PySide QTableWidget and i fill this table with items from QListWidget by running a function:
def fillTable(self):
fruits = self.listWidget.selectedItems() # ['Apple', 'Banana', 'Coconut']
self.tableWidget.setRowCount(len(fruits))
n = 0
for i in fruits:
item = QTableWidgetItem()
self.tableWidget.setItem(n, 0, item)
item.setText(i)
n = n + 1
Running this function again with another items in QListWidget replace items in a table. How to append a new items to existing items?
Solution
To add new elements you must increase the number of rows, and use the new positions. For this reason you must obtain the number of rows before the insertion to use the function rowCount()
as shown below:
def fillTable(self):
fruits = self.listWidget.selectedItems() # ['Apple', 'Banana', 'Coconut']
n = self.tableWidget.rowCount()
self.tableWidget.setRowCount(n + len(fruits))
for i in fruits:
item = QTableWidgetItem()
self.tableWidget.setItem(n, 0, item)
item.setText(i)
n = n + 1
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.