Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting data from QTreeView



  • Hello there,

    I'm using the QTreeView and pyside2. Headers were set up :

            model = QStandardItemModel(0, 2, treeview.parentWidget())
            model.setHeaderData(DUE_DATE, Qt.Horizontal, "Date Due")
            model.setHeaderData(TITLE, Qt.Horizontal, "To Do")
            model.setHeaderData(NOTES, Qt.Horizontal, "Notes")
    

    With DUE_DATE, TITLE and NOTE simple int constants. I added rows by :

            model.insertRow(0)
            model.setData(model.index(0, DUE_DATE), due_date.toString("d MMMM yy @ h:m ap"))
            model.setData(model.index(0, TITLE), title)
    

    NOTES column is hidden.

    I'd like to access the selected row data. I can retrieve the data in the first column with:

    model.data( treeview.currentIndex() )
    

    ... but for the selected row, how can I access the data in the other columns, on the same row (i.e. TITLE and NOTES)

    Alternatively, can someone point me to a basic pyside TreeView example using a QAbstractItemModel, to handle a list of objects (with header and rows, like the above) - as I feel this would be a better approach.

    thanks in advance


  • Lifetime Qt Champion

    Hi,

    You can use the QModelIndex.siblingAtColumn method.


  • Lifetime Qt Champion

    Hi,

    You can use the QModelIndex.siblingAtColumn method.



  • Ah thanks @SGaist didn't spot that in the documentation. Worked perfectly, thanks


Log in to reply