tags:

views:

853

answers:

1

Hello, I'm using QTreeView with QDirModel like this:

QDirModel * model = new QDirModel;
ui->treeView->setModel(model);
ui->treeView->setSelectionMode(QTreeView::ExtendedSelection);
ui->treeView->setSelectionBehavior(QTreeView::SelectRows);

This works fine, however, I'm not sure how to get the details about the files I select. I've got this so far:

QModelIndexList list = ui->treeView->selectionModel()->selectedIndexes();

But not sure what to do now, I'd like to get each file's name and full path. An example would be really great. Thank you.

+2  A: 

you can use fileInfo method of the QDirModel to get file details for the given model index object, smth like this:

QModelIndexList list = ui->treeView->selectionModel()->selectedIndexes();
QDirModel* model = (QDirModel*)ui->treeView->model();
int row = -1;
foreach (QModelIndex index, list)
{
    if (index.row()!=row && index.column()==0)
    {
        QFileInfo fileInfo = model->fileInfo(index);
        qDebug() << fileInfo.fileName() << '\n';
        row = index.row();
    }
}

hope this helps, regards

serge_gubenko
Just beat me to it... A small detail: since the selection is set to be rows at a time, you can get the selection list as a list of each row's first column's model index, instead of all of the selected indexes. See the selectedRows() function at http://doc.trolltech.com/4.5/qitemselectionmodel.html#selectedRows
Caleb Huitt - cjhuitt
Thank you, guys!
Keiji