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

查看:376
本文介绍了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..

推荐答案

您可以尝试为文件对话框设置代理模型: 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天全站免登陆