How to make tableWidget.setItem accept a python list?
-
I have a table that I fill with data from a JSON file.
But because one of the values is an array(that gets converted to a python list) I get an error.
How can I make it so that a tableWidget.setItem accepts a python list as a value for the cell of the table?
Previously I simply converted it to a string, and put that string into the QTableWidgetItem.
But I want to be able to later extract that data from a cell as a list, so that then I can convert it to JSON and save it.
I read the documentation, and supposedly this could be related to Subclassing, but I could not understand how it works.Here is the part of my code:
for r in range(len(db_account_rows)): for c in range(len(db_account_columns)): self.tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem( db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]] # f'{db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]]}' ))
The commented part is how it was previously.
And the uncommented part seems to work right until it encounters a python list in the dict that was created from JSON. -
I have a table that I fill with data from a JSON file.
But because one of the values is an array(that gets converted to a python list) I get an error.
How can I make it so that a tableWidget.setItem accepts a python list as a value for the cell of the table?
Previously I simply converted it to a string, and put that string into the QTableWidgetItem.
But I want to be able to later extract that data from a cell as a list, so that then I can convert it to JSON and save it.
I read the documentation, and supposedly this could be related to Subclassing, but I could not understand how it works.Here is the part of my code:
for r in range(len(db_account_rows)): for c in range(len(db_account_columns)): self.tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem( db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]] # f'{db_bot_accounts[list(db_account_rows)[r]][list(db_account_columns)[c]]}' ))
The commented part is how it was previously.
And the uncommented part seems to work right until it encounters a python list in the dict that was created from JSON.@Lord-Emperor QTableWidgetItem has no constructor which takes any list/array.
You can use https://doc.qt.io/qt-6/qtablewidgetitem.html#setData to set a QVariant, but the question is: what should the table cell do with your list? How should it be shown? -
@Lord-Emperor QTableWidgetItem has no constructor which takes any list/array.
You can use https://doc.qt.io/qt-6/qtablewidgetitem.html#setData to set a QVariant, but the question is: what should the table cell do with your list? How should it be shown?@jsulm
Same as I did before, this list ca be represented simply as text.
Because I know this list is always going to be small.
"["item1", "item2", "item3", "item4"]"
But be stored in the background the same way as it was given to the cell.
So that when using self.tableWidget.item(_row, _column) I would get the exact same list I put in, and not a string of text.
How would one achieve that?
If possible can you please provide an example? I'm really new to QT, and a lot of concepts are way too new for me. -
@jsulm
Same as I did before, this list ca be represented simply as text.
Because I know this list is always going to be small.
"["item1", "item2", "item3", "item4"]"
But be stored in the background the same way as it was given to the cell.
So that when using self.tableWidget.item(_row, _column) I would get the exact same list I put in, and not a string of text.
How would one achieve that?
If possible can you please provide an example? I'm really new to QT, and a lot of concepts are way too new for me.Hi,
There's currently no real benefit with what you ask.
QTableWidgetItem does not handle lists the way you want it. You would also need a custom delegate to render that list which, while not complicated, would mean a lot of list to string operation. This mean burning CPUs for nothing. Doing a join when creating the item and a split when you want to retrieve the data is both simple and cost effective.
-