2017-04-11 33 views
0

我正在使用QFileSystemModel和QTreeView,我試圖讓TreeView默認選擇第一個文件夾/文件。爲此,我需要獲取第一個文件夾/文件的索引,但我找不到在QFileSystemModel中執行此操作的方法。在QFileSystemModel中查找第n個文件/文件夾

你能幫我嗎?

預先感謝您。


我試過setCurrentIndex(_model->index(x, y))但它沒有工作。下面是我的代碼,並顯示該樹:

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()); 
} 

輸出:

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

Tree

+1

'model-> index(x,y)'中的第二個參數不代表子項目。它實際上代表了修改日期,文件大小,名稱等項目的列。 請查看我的答案,以瞭解如何獲取父索引的子索引。 – mrg95

回答

0

的方法不在模式,而是在視圖中。

QTreeView::setCurrentIndex 

從文檔:

QAbstractItemView中:: setCurrentIndex(常量QModelIndex &索引)設置 當前項是在索引的項。

除非當前選擇模式爲NoSelection,否則該項目也會被選中 。請注意,此功能還會爲用戶執行的任何新選擇更新起始位置 。

要設置的項作爲當前項但不選擇它,呼叫

selectionModel的() - > setCurrentIndex(索引, QItemSelectionModel :: NOUPDATE);

另請參閱currentIndex(),currentChanged()和selectionMode。

的第一個文件夾中的代碼是不是真的容易,乍看之下,但你要記住,對模型的數據抽象使得它強大的,並且cumberstone在同一時間,所以任何項目可能是一個文件夾或文件,我們需要檢查的是:

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. 
    } 
} 
+1

你好,感謝你的快速回復。其實,我不在乎它是一個文件還是一個文件夾,我只想選擇樹中的第一個項目。請看看最新的問題。 –

+0

他的回答是過分複雜的事情。他通過調用'QStandardItemModel :: index(row,column,parentIndex)'來調用'QModelIndex :: child()'。他究竟在哪裏回來?你還說你不關心這個物品的類型? – mrg95

+0

暗示這是在函數調用中,至少我認爲它是隱含的。 :)他說(在編輯之前(我想擁有第一個文件,因爲第一個文件可能在一連串文件夾中,這是我能想到的最快解決方案。) –

0

爲了獲得在模型上的特定位置的索引,使用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)); 

我可以從你的追求告訴你不明白樹視圖的行和列系統是如何工作的。請閱讀documentation

+0

請參閱更新的問題,idx。 child(0,0)'does not really work ... –

+0

我編輯了我的答案。因爲你沒有設置根索引,所以你仍然可以在'model-index'的索引處調用'QModelIndex :: child() > index(0,0)' 我可以從你的問題中知道你不明白樹視圖的行和列系統是如何工作的,請閱讀文檔:http://doc.qt.io/qt-4.8/模型 - 視圖 - programming.html#模型類 – mrg95

相關問題