QTreeView 仅显示父目录,而不显示父目录及其所有兄弟目录

发布于 2024-11-17 12:46:21 字数 171 浏览 6 评论 0原文

我试图让 QTreeView (使用底层 QFileSystemModel)显示目录树。如果我将 RootPath 设置为父目录,那么我会看到所有子目录,但看不到父目录。如果我将 RootPath 设置为父目录的父目录,那么我会看到父目录及其所有同级目录。有没有办法让它显示没有兄弟姐妹的父母以及所有孩子?

谢谢

I'm trying to get QTreeView (using an underlying QFileSystemModel) to show a directory tree. If I set the RootPath to the parent directory, then I see all the children, but not the parent. If I set the RootPath to be the parent's parent, then I see the parent directory with all its siblings. Is there a way to get it to show the parent without its siblings, and all the children?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-11-24 12:46:21

这对我在 Linux 上有效。我并不是说这是最好的实现,并且我不确定使用反斜杠分隔符是否适用于 Windows。我知道 Qt 将它们转换为本机分隔符,但我不知道它是否是来自模型的 data 方法的本机分隔符。

#include <QApplication>
#include <QFileSystemModel>
#include <QSortFilterProxyModel>
#include <QTreeView>

class FilterModel : public QSortFilterProxyModel
{
public:
    FilterModel( const QString& targetDir ) : dir( targetDir )
    {
        if ( !dir.endsWith( "/" ) )
        {
            dir += "/";
        }
    }

protected:
    virtual bool filterAcceptsRow( int source_row
                                 , const QModelIndex & source_parent ) const
    {
        QString path;
        QModelIndex pathIndex = source_parent.child( source_row, 0 );
        while ( pathIndex.parent().isValid() )
        {
            path = sourceModel()->data( pathIndex ).toString() + "/" + path;
            pathIndex = pathIndex.parent();
        }
        // Get the leading "/" on Linux. Drive on Windows?
        path = sourceModel()->data( pathIndex ).toString() + path;

        // First test matches paths before we've reached the target directory.
        // Second test matches paths after we've passed the target directory.
        return dir.startsWith( path ) || path.startsWith( dir );
    }

private:
    QString dir;
};

int main( int argc, char** argv )
{
    QApplication app( argc, argv );

    const QString dir( "/home" );
    const QString targetDir( dir + "/sample"  );

    QFileSystemModel*const model = new QFileSystemModel;
    model->setRootPath( targetDir );

    FilterModel*const filter = new FilterModel( targetDir );
    filter->setSourceModel( model );

    QTreeView*const tree = new QTreeView();
    tree->setModel( filter );
    tree->setRootIndex( filter->mapFromSource( model->index( dir ) ) );
    tree->show();

    return app.exec();
}

This works for me on Linux. I'm not claiming it's the best implementation and I'm not sure if using backslash separators will work on Windows. I know Qt translates them to the native separator but I don't know if it's native separators that come out of the model's data method.

#include <QApplication>
#include <QFileSystemModel>
#include <QSortFilterProxyModel>
#include <QTreeView>

class FilterModel : public QSortFilterProxyModel
{
public:
    FilterModel( const QString& targetDir ) : dir( targetDir )
    {
        if ( !dir.endsWith( "/" ) )
        {
            dir += "/";
        }
    }

protected:
    virtual bool filterAcceptsRow( int source_row
                                 , const QModelIndex & source_parent ) const
    {
        QString path;
        QModelIndex pathIndex = source_parent.child( source_row, 0 );
        while ( pathIndex.parent().isValid() )
        {
            path = sourceModel()->data( pathIndex ).toString() + "/" + path;
            pathIndex = pathIndex.parent();
        }
        // Get the leading "/" on Linux. Drive on Windows?
        path = sourceModel()->data( pathIndex ).toString() + path;

        // First test matches paths before we've reached the target directory.
        // Second test matches paths after we've passed the target directory.
        return dir.startsWith( path ) || path.startsWith( dir );
    }

private:
    QString dir;
};

int main( int argc, char** argv )
{
    QApplication app( argc, argv );

    const QString dir( "/home" );
    const QString targetDir( dir + "/sample"  );

    QFileSystemModel*const model = new QFileSystemModel;
    model->setRootPath( targetDir );

    FilterModel*const filter = new FilterModel( targetDir );
    filter->setSourceModel( model );

    QTreeView*const tree = new QTreeView();
    tree->setModel( filter );
    tree->setRootIndex( filter->mapFromSource( model->index( dir ) ) );
    tree->show();

    return app.exec();
}
笑红尘 2024-11-24 12:46:21

QTreeView 描述中的 示例 讨论了调用 QFileSystemModel::setRootPath 但描述 QFileSystemModel 讨论使用 QTreeView::setRootIndex。该文件说:

特定目录的内容
可以通过设置树来显示
视图的根索引

这是一个对我有用的示例。在此示例中,当我没有调用 tree->setRootIndex(model->index((dir))) 时,它用于向我显示所有目录的列表,而不是 c :/样本。希望这有帮助。

#include <QtGui/QApplication>
#include <QtGui>
#include <QFileSystemModel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFileSystemModel *model = new QFileSystemModel;
    QString dir("c:/sample");
    model->setRootPath(dir);
    QTreeView *tree = new QTreeView();
    tree->setModel(model);
    tree->setRootIndex(model->index((dir)));
    tree->show();
    return a.exec();
}

The example in description of QTreeView talks about calling QFileSystemModel::setRootPath but description of QFileSystemModel talks of using QTreeView::setRootIndex. The document says:

the contents of a particular directory
can be displayed by setting the tree
view's root index

Here is an example which worked for me. In this example when I wasn't calling tree->setRootIndex(model->index((dir))), it used to show me list of all the directories and not c:/sample. Hope this helps.

#include <QtGui/QApplication>
#include <QtGui>
#include <QFileSystemModel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFileSystemModel *model = new QFileSystemModel;
    QString dir("c:/sample");
    model->setRootPath(dir);
    QTreeView *tree = new QTreeView();
    tree->setModel(model);
    tree->setRootIndex(model->index((dir)));
    tree->show();
    return a.exec();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文