【问题标题】:QFileDialog for directories that have certain contentQFileDialog 用于具有特定内容的目录
【发布时间】:2010-07-20 22:31:53
【问题描述】:

我想构建一个类似于QFileDialog::getExistingDirectory() 的对话框,仅当所选目录包含某些文件时,才会启用该对话框。

我知道我无法通过 QFileDialog 实现这一点,而是我必须想出我自己的 QDialog,它有一个 QTreeView 与一个 QFileSystemModel 耦合。

  1. 如何将QTreeView 限制为目录?
  2. 如何获取当前选择的目录,以便检查它是否包含一些文件名?

【问题讨论】:

  • 我的回答对你不起作用吗?
  • 仍在实施中,抱歉耽搁了...

标签: qt qtreeview qfiledialog


【解决方案1】:
  1. 在 QFileSystemModel 上使用 setFilter 与 QDir::AllDirs 或 QDir::Dirs 选项,可能是前者。
  2. 将树视图中的activated(QModelIndex) 信号连接到您的自定义插槽。在此插槽中,将 QModelIndex 传递给模型的 fileInfo/filePath 方法,以检索所选目录的信息/路径,然后执行检查

这是一个例子:

void slotDirectorySelected( const QModelIndex & index )
{
    QFileInfo info = fileSystemModel->fileInfo( index );
    QDir selectedDir = info.dir();
    foreach( const QString entry, selectedDir.entryList() ) {
        // do something with the entry
    }
}

【讨论】:

  • 谢谢,这行得通!我决定用子类 QSotFilterProxyModel 包装 QFileSystemModel 以便能够排序并且不显示 QTreeView 中目录名称旁边的任何列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 2022-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多