qfiledialog - 过滤文件夹? [英] qfiledialog - Filtering Folders?

查看:25
本文介绍了qfiledialog - 过滤文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我想获取文件夹监控应用程序的文件夹名称..有没有办法可以过滤掉使用 QFileDialog 显示的特定文件夹(例如,我不希望我的文档显示在文件对话框中).

1)I want to get the name of the folder for a folder monitoring Application.. Is there a way that i can filter out specific folders from being displayed using QFileDialog (For example i don't want the my documents to be displayed in the file dialog)..

2) 我不希望用户选择驱动器.默认情况下,在此代码中也可以选择驱动器..

2)I don't want the user to select a drive. By default in this code drives can also be selected..

dirname=QtGui.QFileDialog.getExistingDirectory(self,'Open Directory','c:\',QtGui.QFileDialog.ShowDirsOnly)
print(dirname)

有什么方法可以使驱动器或某些特定文件夹变灰以使其无法被选择,或者我可以设置文件夹的过滤器以防止显示它..

Is there a way that i can gray out the drives or some specific folders so that it can't be selected or can i set the filters for folder to prevent showing it up..

推荐答案

您可以尝试为文件对话框设置代理模型:QFileDialog::setProxyModel.在代理模型类中覆盖 filterAcceptsRow 方法并为文件夹返回 false您不想显示的内容.下面是代理模型的外观示例;它是c ++,如果将此代码转换为python 有任何问题,请告诉我.这个模型应该过滤掉文件并只显示文件夹:

You can try setting a proxy model for your file dialog: QFileDialog::setProxyModel. In the proxy model class override the filterAcceptsRow method and return false for folders which you don't want to be shown. Below is an example of how proxy model can look like; it'c c++, let me know if there are any problems converting this code to python. This model is supposed to filter out files and show only folders:

class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
    virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};

bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
    QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());

    if (fileModel!=NULL && fileModel->isDir(index0))
    {
        qDebug() << fileModel->fileName(index0);
        return true;
    }
    else
        return false;
    // uncomment to execute default implementation
    //return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}

我是这样称呼它的

QFileDialog dialog;
FileFilterProxyModel* proxyModel = new FileFilterProxyModel;
dialog.setProxyModel(proxyModel);
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.exec();

请注意,仅非本机文件对话框支持代理模型.

Note that the proxy model is supported by non-native file dialogs only.

这篇关于qfiledialog - 过滤文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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