QModelIndexList: getting 4 QModelIndex for each item selected in a QTreeView.
In the following code, why am I getting size 4 for QModelIndexList if I click in one file or directory only? How can I get only one path? I know I could use [0] but is there another way to get size 1?
Code:
// fsModel = QFileSystemModel
{
ui->setupUi(this);
const QString rootPath
= "/home/rob/";
fsModel = new QFileSystemModel(this);
fsModel->setRootPath(rootPath);
ui->treeView->setModel(fsModel);
ui->treeView->setRootIndex(fsModel->index(rootPath));
}
void MainWindow::on_buttonCompress_clicked()
{
QModelIndexList list = ui->treeView->selectionModel()->selectedIndexes();
qDebug() << list.size();
qDebug() << fsModel->filePath(modelIndex);
// I wouldn't like to use []. I would like to get only one filepath, the one I selected.
qDebug() << fsModel->filePath(list[0]);
}
Re: QModelIndexList: getting 4 QModelIndex for each item selected in a QTreeView.
What are you using for your QTreeView::selectionBehavior()? SelectRows? If so, the selection model will return all QModelIndex items for each column in the row.
If you examine the QModelIndex entries in the list, I think you should see that they all have the same row() and parent() but different column() values.