使用 QFileSystemModel 同步具有不同元素的两个视图
Using QFileSystemModel to synchronise two views with different elements
我正在使用左侧的 QTreeView
和 right/main 侧使用 QListView
的目录树/导航窗格和 right/main 的图标视图来构建类似浏览器的视图.树侧应该只显示目录(最好是非空目录……但那是另一回事),图标视图只显示具有特定名称过滤器且没有目录的文件。我正在努力如何正确地做到这一点。
首先:我不知道是否应该为此使用一个或两个 QFileSystemModels
。使用一个我必须为每个视图使用两个附加的 QSortFilterProxyModels
进行过滤 — 但我不再有文件系统属性......并且仅使用 RegEx 是一种限制。使用两个模型已被证明很难,因为我无法真正将 QModelIndex
从一个模型映射到另一个模型,因为模型包含不同的项目。例如,当我单击左侧的目录时,右侧的根路径应该得到更新。但是该目录不包含在模型中......所以这不起作用。
关于如何正确执行此操作的任何想法?谢谢!
How can the file directory tree view and the file navigation view interact?
不坚持这是唯一的方法,但对我有用:
- 一个视图模型只过滤目录,另一个只过滤文件
- 每个视图(目录树和文件导航)都使用指向某个根的相同类型的单独文件模型对象
- 使用文件系统中的路径同步视图:当用户单击其中一个视图更改选择(在您的情况下为树视图)时,另一个正在从同一个视图中拾取路径
- 请注意,
QSortFilterProxyModel
我们首先必须从那里获取当前视图位置
void MyFileSystemWidget::startViews()
{
// First, initialize QTreeView and QTableView each with own
// QFileSystemModel/QSortFilterProxyModel(MySortFilterProxyModel)
// with individual selection e.g. QDir::Files or QDir::Dirs
//// //// ////
// Make models to point at the same root to start
const QModelIndex rootTreeViewIdx = m_pTreeSortFilterProxyModel->mapFromSource( m_pTreeDataModel->index(rootDir.path()) );
m_pTreeView->setRootIndex(rootTreeViewIdx);
m_pTreeView->setCurrentIndex(rootTreeViewIdx);
const QModelIndex rootFileViewIdx = m_pListSortFilterProxyModel->mapFromSource( m_pListDataModel->index(rootDir.path()) );
m_pTableView->setRootIndex(rootFileViewIdx);
// now connect tree view clicked signal
connect(m_pTreeView, SIGNAL(clicked(QModelIndex)), SLOT(onTreeViewClicked(QModelIndex)));
}
void MyFileSystemWidget::onTreeViewClicked(const QModelIndex& treeIndex)
{
// see MySortFilterProxyModel::sourceFileInfo
QString modelPath = m_pTreeSortFilterProxyModel->sourceFileInfo( treeIndex ).absoluteFilePath();
if (modelPath.isEmpty())
return;
// see MySortFilterProxyModel::setSourceModelRootPath
const QModelIndex proxyIndex = m_pListSortFilterProxyModel->setSourceModelRootPath(modelPath);
m_pTableView->setRootIndex(proxyIndex);
}
QFileInfo MySortFilterProxyModel::sourceFileInfo(const QModelIndex& index)
{
if (!index.isValid())
return QFileInfo();
const QModelIndex proxyIndex = mapToSource( index );
if (!proxyIndex.isValid())
return QFileInfo();
return static_cast<QFileSystemModel*>(sourceModel())->fileInfo(proxyIndex);
}
QModelIndex MySortFilterProxyModel::setSourceModelRootPath(const QString& modelPath)
{
const QModelIndex fmIndex = static_cast<QFileSystemModel*>(sourceModel())->setRootPath(modelPath);
return mapFromSource( fmIndex );
}
我正在使用左侧的 QTreeView
和 right/main 侧使用 QListView
的目录树/导航窗格和 right/main 的图标视图来构建类似浏览器的视图.树侧应该只显示目录(最好是非空目录……但那是另一回事),图标视图只显示具有特定名称过滤器且没有目录的文件。我正在努力如何正确地做到这一点。
首先:我不知道是否应该为此使用一个或两个 QFileSystemModels
。使用一个我必须为每个视图使用两个附加的 QSortFilterProxyModels
进行过滤 — 但我不再有文件系统属性......并且仅使用 RegEx 是一种限制。使用两个模型已被证明很难,因为我无法真正将 QModelIndex
从一个模型映射到另一个模型,因为模型包含不同的项目。例如,当我单击左侧的目录时,右侧的根路径应该得到更新。但是该目录不包含在模型中......所以这不起作用。
关于如何正确执行此操作的任何想法?谢谢!
How can the file directory tree view and the file navigation view interact?
不坚持这是唯一的方法,但对我有用:
- 一个视图模型只过滤目录,另一个只过滤文件
- 每个视图(目录树和文件导航)都使用指向某个根的相同类型的单独文件模型对象
- 使用文件系统中的路径同步视图:当用户单击其中一个视图更改选择(在您的情况下为树视图)时,另一个正在从同一个视图中拾取路径
- 请注意,
QSortFilterProxyModel
我们首先必须从那里获取当前视图位置
void MyFileSystemWidget::startViews()
{
// First, initialize QTreeView and QTableView each with own
// QFileSystemModel/QSortFilterProxyModel(MySortFilterProxyModel)
// with individual selection e.g. QDir::Files or QDir::Dirs
//// //// ////
// Make models to point at the same root to start
const QModelIndex rootTreeViewIdx = m_pTreeSortFilterProxyModel->mapFromSource( m_pTreeDataModel->index(rootDir.path()) );
m_pTreeView->setRootIndex(rootTreeViewIdx);
m_pTreeView->setCurrentIndex(rootTreeViewIdx);
const QModelIndex rootFileViewIdx = m_pListSortFilterProxyModel->mapFromSource( m_pListDataModel->index(rootDir.path()) );
m_pTableView->setRootIndex(rootFileViewIdx);
// now connect tree view clicked signal
connect(m_pTreeView, SIGNAL(clicked(QModelIndex)), SLOT(onTreeViewClicked(QModelIndex)));
}
void MyFileSystemWidget::onTreeViewClicked(const QModelIndex& treeIndex)
{
// see MySortFilterProxyModel::sourceFileInfo
QString modelPath = m_pTreeSortFilterProxyModel->sourceFileInfo( treeIndex ).absoluteFilePath();
if (modelPath.isEmpty())
return;
// see MySortFilterProxyModel::setSourceModelRootPath
const QModelIndex proxyIndex = m_pListSortFilterProxyModel->setSourceModelRootPath(modelPath);
m_pTableView->setRootIndex(proxyIndex);
}
QFileInfo MySortFilterProxyModel::sourceFileInfo(const QModelIndex& index)
{
if (!index.isValid())
return QFileInfo();
const QModelIndex proxyIndex = mapToSource( index );
if (!proxyIndex.isValid())
return QFileInfo();
return static_cast<QFileSystemModel*>(sourceModel())->fileInfo(proxyIndex);
}
QModelIndex MySortFilterProxyModel::setSourceModelRootPath(const QString& modelPath)
{
const QModelIndex fmIndex = static_cast<QFileSystemModel*>(sourceModel())->setRootPath(modelPath);
return mapFromSource( fmIndex );
}