Search code examples
pythonpython-3.xpyqtpyqt5qstandarditemmodel

retrieve a specific data from the selected index in a TreeModel


I used a custom model to populate theTreeView and tried using

        for ix in self.dataView.selectedIndexes():
            text = ix.data()  # or ix.data()
            print(text)

but this prints all the data in that column(index)...here is a picture of the model i used model = QStandardItemModel(0, 3, parent), model used

My problem is i don't need all the data, i need the data of the 3rd row (specific data) i.e the filepath

this is the output, using the later code

We & Love.txt
11.630% 
C:\Users\Black Laptop\Desktop\Work\We & Love.txt

i just need the 3rd data, not all, thanks


Solution

  • The QModelIndex are associated to each item, in your case you have the one of the complete row, so the solution is to filter by the column:

    for ix in self.dataView.selectedIndexes():
        # the indexes of the column start at 0 so the 3rd column has index 2
        if ix.column() == 2:
            text = ix.data()
            print(text)