Issue
I have ListView and iterating over a dictionary. I want to display the index number per row in the view.
model = QtGui.QStandardItemModel()
self.listView.setModel(model)
results = {'D2_SMI_1': True, 'D2_SMI_2': False}
for key, value in results.items():
item = QStandardItem(key)
item.setCheckable(True)
item.setCheckState(value)
model.appendRow(item)
Solution
A possible solution is to add the number through a delegate:
import sys
from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QApplication, QListView, QStyledItemDelegate
class Delegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super().initStyleOption(option, index)
option.text = f"{index.row() + 1}. {option.text}"
def main():
app = QApplication(sys.argv)
model = QStandardItemModel()
for text in ("D2_SMI_1", "D2_SMI_2", "D2_SMI_3", "D2_SMI_4"):
item = QStandardItem(text)
model.appendRow(item)
view = QListView()
view.setModel(model)
view.resize(640, 480)
view.show()
delegate = Delegate(view)
view.setItemDelegate(delegate)
ret = app.exec_()
sys.exit(ret)
if __name__ == "__main__":
main()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.