How to get the selected item in a QTreeView

c++, qt, qtreeview

Solution

You need to call `QItemSelectionModel::selectedIndexes()` instead, i.e.:

QModelIndexList indexes = ui->treeView->selectionModel()->selectedIndexes();
if (indexes.size() > 0) {
    QModelIndex selectedIndex = indexes.at(0);
    [..]
}

Problem

I have a tree like this: |-Parent | |-Child-Child |-Parent | |-Child-Child ... Only the Parents are selectable. How can I get the data from the selected Parent? I tried ``` ui->treeView->selectedIndexes()[0]; ``` but it says that selectedIndexes() is protected.

Original source