在 QFileSystemModel 中找到第 n 个 file/folder
Find nth file/folder in QFileSystemModel
我正在使用 QFileSystemModel 和 QTreeView,并且我正在尝试使 TreeView select 默认成为第一个 folder/file。为此,我需要获取第一个 folder/file 的索引,但我找不到在 QFileSystemModel 中执行此操作的方法。
你能帮帮我吗?
提前致谢。
我试过 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"
方法不在模型中,而是在视图中。
QTreeView::setCurrentIndex
来自文档:
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.
第一个文件夹的代码乍一看并不简单,但您需要记住,模型上的数据抽象使其功能强大,同时也是笨重的,因此任何项目都可能是一个文件夹或一个文件,我们需要检查它:
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.
}
}
为了获取模型中特定位置的索引,使用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
我正在使用 QFileSystemModel 和 QTreeView,并且我正在尝试使 TreeView select 默认成为第一个 folder/file。为此,我需要获取第一个 folder/file 的索引,但我找不到在 QFileSystemModel 中执行此操作的方法。
你能帮帮我吗?
提前致谢。
我试过 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"
方法不在模型中,而是在视图中。
QTreeView::setCurrentIndex
来自文档:
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.
第一个文件夹的代码乍一看并不简单,但您需要记住,模型上的数据抽象使其功能强大,同时也是笨重的,因此任何项目都可能是一个文件夹或一个文件,我们需要检查它:
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.
}
}
为了获取模型中特定位置的索引,使用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