ファイル ダイアログのプロキシ モデルを設定してみることができます: QFileDialog::setProxyModel。プロキシ モデル クラスで、filterAcceptsRowメソッドをオーバーライドし、表示したくないフォルダーに対して false を返します。以下は、プロキシ モデルがどのように見えるかの例です。それは c++ です。このコードを Python に変換する際に問題がある場合はお知らせください。このモデルは、ファイルを除外してフォルダーのみを表示することになっています。
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();
プロキシ モデルは、非ネイティブ ファイル ダイアログでのみサポートされていることに注意してください。