简体   繁体   中英

Find nth file/folder in QFileSystemModel

I am using a QFileSystemModel and a QTreeView and I am trying to make the TreeView select the first folder/file by default. To do that I need to get the index of the first folder/file but I can't find a method that does that in QFileSystemModel.

Could you please help me?

Thank you in advance.


I have tried setCurrentIndex(_model->index(x, y)) but it didn't work. Here's the code I have and the tree shown:

void CodeView::finished_loading(QString file) {
        qDebug()<<"Currently selected : " << _model->fileName( ui->treeView->currentIndex());
        qDebug()<<"(0,0) "<< _model->fileName(_model->index(0,0));
        qDebug()<<"(1,0) "<< _model->fileName(_model->index(1,0));
        qDebug()<<"(2,0) "<< _model->fileName(_model->index(2,0));
        qDebug()<<"(3,0) "<< _model->fileName(_model->index(3,0));
        qDebug()<<"(0,0) "<< _model->fileName(_model->index(0,0));
        qDebug()<<"(1,1) "<< _model->fileName(_model->index(1,1));
        qDebug()<<"(2,1) "<< _model->fileName(_model->index(2,1));
        qDebug()<<"(3,1) "<< _model->fileName(_model->index(3,1));
        ui->treeView->setCurrentIndex(_model.index(1,0));
        qDebug()<<"New selected : " << _model->fileName( ui->treeView->currentIndex());
}

Output :

Currently selected :  "Wassim Gharbi"
(0,0)  "/"
(1,0)  ""
(2,0)  ""
(3,0)  ""
(0,0)  "/"
(1,1)  ""
(2,1)  ""
(3,1)  ""
New selected :  "Wassim Gharbi"

树

The method is not in the model, but in the view.

 QTreeView::setCurrentIndex

from the docs:

QAbstractItemView::setCurrentIndex(const QModelIndex &index) Sets the current item to be the item at index.

Unless the current selection mode is NoSelection, the item is also selected. Note that this function also updates the starting position for any new selections the user performs.

To set an item as the current item without selecting it, call

selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);

See also currentIndex(), currentChanged(), and selectionMode.

The code for the first folder is not really easy as first glance but you need to remember that the data abstractions on the models makes it powerfull, and cumberstone at the same time, so any item could potentially be a folder or a file, and we need to check for that:

if (model->rowCount()) // has at least one file or folder
{
   QModelIndex current = model->index(0,0);
   if (model->rowCount(current) == 0); // it's a file.
      return current;
   else {
      // walk the tree trying to find the first file on the folders.
      while(model->rowCount(current) > 0) {
          current = model->index(0,0,current);
      }
      if (index.isValid())
         return index; // our file inside folders
      else 
         return QModelIndex(); // no file inside folders.
   }
}

In order to get the index at a specific location in the model, use QModelIndex::child(row, column)

QFileSystemModel *model = new QFileSystemModel();
model->setRootPath("C:/Qt");//your path

ui->treeView->setModel(model);
ui->treeView->setCurrentIndex(model->index(0, 0).child(0, 0));

I can tell from your question that you don't understand how a treeview's row and column system works. Please read the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM