7

1) フォルダー監視アプリケーションのフォルダーの名前を取得したい.. QFileDialog を使用して特定のフォルダーを表示から除外する方法はありますか (たとえば、私のドキュメントをファイル ダイアログ)。

2) ユーザーにドライブを選択させたくありません。このコードでは、デフォルトでドライブも選択できます。

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

ドライブまたは特定のフォルダーをグレー表示して選択できないようにする方法はありますか、またはフォルダーのフィルターを設定して表示されないようにすることはできますか..

4

4 に答える 4

8

ファイル ダイアログのプロキシ モデルを設定してみることができます: 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();

プロキシ モデルは、非ネイティブ ファイル ダイアログでのみサポートされていることに注意してください。

于 2010-01-24T05:18:17.683 に答える
1

QDir.Dirs フィルターを使用してみることができます。

dialog = QtGui.QFileDialog(parentWidget)

dialog.setFilter(QDir.Dirs)
于 2010-01-22T04:09:36.070 に答える
1

serge_gubenko さんが正しい答えをくれました。フォルダ名を確認し、表示しないものについては「false」を返すだけで済みました。たとえば、「private」という名前のフォルダーを除外するには、次のように記述します。

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

私はすでにこれをテストしましたが、完全に機能します。serge_gubenko は当然のクレジットをすべて受け取るはずです。

于 2010-04-30T14:16:05.853 に答える