qfiledialog - Filtering Folders?

柔情痞子 提交于 2019-12-17 07:52:28

问题


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)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..


回答1:


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

here's how I was calling it

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.




回答2:


You can try using QDir.Dirs filter.

dialog = QtGui.QFileDialog(parentWidget)

dialog.setFilter(QDir.Dirs)



回答3:


serge_gubenko gave you the right answer. You only had to check the folder names and return "false" for the ones which should not be displayed. For instance, to filter out any folders named "private" you'd write:

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);
        if (QString::compare(fileModel->fileName(index0), tr("private")) == 0)
            return false;

        return true;
    }
    else
        return false;
    // uncomment to execute default implementation
    //return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}

I already tested this and it works perfectly. serge_gubenko should receive all due credit.



来源:https://stackoverflow.com/questions/2101100/qfiledialog-filtering-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!