QFileDialog用于具有特定内容的目录 [英] QFileDialog for directories that have certain content

查看:97
本文介绍了QFileDialog用于具有特定内容的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个类似于 QFileDialog :: getExistingDirectory()的对话框(仅当所选目录包含某些文件时才启用确定"按钮).

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.

我知道我无法通过 QFileDialog 实现这一目标,相反,我会拥有提出自己的QDialog,该对话框具有 QTreeView QFileSystemModel .

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. 如何限制 QTreeView 到目录?
  2. 如何获取当前选择的目录,以便检查其中是否包含一些文件名?
  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?

推荐答案

  1. 在QFileSystemModel上将 setFilter 与QDir一起使用: AllDirs或QDir :: Dirs选项,可能是前者.
  2. activated(QModelIndex)信号从树形视图连接到您的自定义广告位.在此插槽中,将QModelIndex传递给模型的fileInfo/filePath方法,以检索所选目录的信息/路径,然后执行检查
  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

这里是一个例子:

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
    }
}

这篇关于QFileDialog用于具有特定内容的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆