QFileDialog 用于具有特定内容的目录

发布于 2024-09-10 10:47:04 字数 698 浏览 1 评论 0原文

我想构建一个类似于 QFileDialog::getExistingDirectory() 仅当所选目录包含某些文件时才启用“确定”按钮。

我知道我无法通过 QFileDialog 实现这一点,相反,我必须提出使用我自己的 QDialog,它有一个 QTreeView 耦合到 QFileSystemModel

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

I would like to build a dialog similar to QFileDialog::getExistingDirectory() for which the OK-button only is enabled when the selected directory contains certain files.

I know I cannot achieve this with QFileDialog, instead I would have to come up with my own QDialog that has a QTreeView coupled to a QFileSystemModel.

  1. How can I limit the QTreeView to directories?
  2. How can I get the currently selected directory so I can check whether it contains some filenames?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-09-17 10:47:04
  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
    }
}
  1. Use setFilter on the QFileSystemModel with either the QDir::AllDirs or QDir::Dirs option, probably the former.
  2. connect the activated(QModelIndex) signal from the treeview to a custom slot of yours. In this slot pass the QModelIndex to the model's fileInfo/filePath method, to retrieve the info/path for the selected directory, then perform your check

Here is an example:

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文