Issue
I have such output:
{'heatpump': [{'Topic': 'TOP0', 'Name': 'Heatpump_State', 'Value': '1', 'Description': 'On'}, {'Topic': 'TOP1', 'Name': 'Pump_Flow', 'Value': '9.08', 'Description': 'l/min'},{.........}, {.......}]}
How to fill QTableWidget if we make such keys
keys = ["Topic", "Name", "Value", "Description"]
And how to make searchable by Name value, use QLabel field to achieve this ?
Adding part of code, how to enumerate rows here and add to the table all values and not the last one ?:
from PySide2 import QtWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
data = {'heatpump': [{'Topic': 'TOP0', 'Name': 'Heatpump_State', 'Value': '1', 'Description': 'On'}, {'Topic': 'TOP1', 'Name': 'Pump_Flow', 'Value': '9.08', 'Description': 'l/min'}]}
d = data
keys = ["Topic", "Name", "Value", "Description"]
labels = keys + ["ID"]
#i = 0
w = QtWidgets.QTableWidget(5, len(labels))
w.setColumnHidden(4, True)
w.setHorizontalHeaderLabels(labels)
for record in d["heatpump"]:
print(record)
for i, (name, value) in enumerate(record.items()):
print(i, name, value)
it = QtWidgets.QTableWidgetItem(value)
w.setItem(0, i, it)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Solution
If you know the "header" labels (the keys), just iter through them to get the corresponding value:
keys = "Topic", "Name", "Value", "Description"
for row, record in enumerate(d["heatpump"]):
print(record)
for column, key in enumerate(keys):
it = QtWidgets.QTableWidgetItem(record.get(key, ""))
w.setItem(row, column, it)
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.