qt 之 QFileSystemModel

Qt QTreeView 详解 一.常见接口使用 1.设置表头隐藏,设置表头 QStandardItemModel *model = new QStandardItemModel(this); //设置表头隐藏 //ui->treeView->setHeaderHidden(true); //设置表头 model->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄"); //设置model ui->tr 阅读详情

1)显示我设定的目录 

   QFileSystemModel *pModel = new QFileSystemModel();
    pModel->setReadOnly(false);
    pModel->setRootPath("C:\\zib");


    ui->treeView->setModel(pModel);
    ui->treeView->header()->setStretchLastSection(true);
    ui->treeView->header()->setSortIndicator(0,Qt::AscendingOrder);
    ui->treeView->header()->setSortIndicatorShown(true);


    QModelIndex index = pModel->index("C:\\zib");
    ui->treeView->setRootIndex(index);
    ui->treeView->expand(index);
    ui->treeView->scrollTo(index);
    ui->treeView->resizeColumnToContents(0);


以下转载的别人的,等以后有事件再看看

转载1

QFileSystemModel 类似QDitModel,只不过Qt不推荐使用QDirModel,推荐是使用QFileSystemModel,该模型允许我们在view中显示操作系统的目录结构。

  directoryviewer.h文件:

#ifndef DIRECTORYVIEWER_H

#define DIRECTORYVIEWER_H


#include <QtGui/QDialog>

#include <QFileSystemModel>

#include <QTreeView>


class DirectoryViewer : public QDialog

{

    Q_OBJECT


public:

    DirectoryViewer(QWidget *parent = 0);

    ~DirectoryViewer();

private slots:

    void createDirectory();

    void remove();

private:

    QFileSystemModel *model;

    QTreeView *treeView;

};


#endif

  directoryviewer.cpp文件:

#include "directoryviewer.h"

#include <QPushButton>

#include <QVBoxLayout>

#include <QHBoxLayout>

#include <QHeaderView>

#include <QInputDialog>

#include <QMessageBox>


DirectoryViewer::DirectoryViewer(QWidget *parent)

    : QDialog(parent)

{

    model = new QFileSystemModel;

    model->setReadOnly(false);            //设置可以修改

    model->setRootPath(QDir::currentPath());


    treeView = new QTreeView;

    treeView->setModel(model);


    treeView->header()->setStretchLastSection(true);

    treeView->header()->setSortIndicator(0, Qt::AscendingOrder);

    treeView->header()->setSortIndicatorShown(true);

    treeView->header()->setClickable(true);


    QModelIndex index = model->index(QDir::currentPath());

    treeView->expand(index);      //当前项展开

    treeView->scrollTo(index);    //定位到当前项

    treeView->resizeColumnToContents(0);


    QPushButton *createButton = new QPushButton("Create Directory", this);

    QPushButton *removeButton = new QPushButton("Remove", this);


    QHBoxLayout *hlayout = new QHBoxLayout;

    hlayout->addWidget(createButton);

    hlayout->addWidget(removeButton);


    QVBoxLayout *vlayout = new QVBoxLayout;

    vlayout->addWidget(treeView);

    vlayout->addLayout(hlayout);


    setLayout(vlayout);


    connect(createButton, SIGNAL(clicked()), this, SLOT(createDirectory()));

    connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));

}


DirectoryViewer::~DirectoryViewer()

{

}


void DirectoryViewer::createDirectory()

{

    QModelIndex index = treeView->currentIndex();

    if (!index.isValid())

    {

        return;

    }

    QString dirName = QInputDialog::getText(this, tr("Create Directory"), tr("Directory name"));

    if (!dirName.isEmpty())

    {

        if (!model->mkdir(index, dirName).isValid())

        {

            QMessageBox::information(this, tr("Create Directory"), tr("Failed to create the directory"));

        }

    }

}


void DirectoryViewer::remove()

{

    QModelIndex index = treeView->currentIndex();

    if (!index.isValid())

    {

        return;

    }

    bool ok;

    if (model->fileInfo(index).isDir())

    {

        ok = model->rmdir(index);

    }

    else

    {

        ok = model->remove(index);

    }

    if (!ok)

    {

        QMessageBox::information(this, tr("Remove"), tr("Failed to remove %1").arg(model->fileName(index)));

    }

}

  main.cpp文件:

#include <QtGui/QApplication>

#include "directoryviewer.h"


int main(int argc, char *argv[])

{

    QApplication a(argc, argv);

    DirectoryViewer w;

    w.show();

    w.setWindowTitle("QFileSystemModel Demo");

    

    return a.exec();

}

 

转载2

QFileSystemModel will not fetch any files or directories until setRootPath() is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows.

QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried.

Calls to rowCount() will return 0 until the model populates a directory. QFileSystemModel keeps a cache with file information. The cache is automatically kept up to date using the QFileSystemWatcher.


In this example, we'll use Qt Gui application with QDialog:

As we discussed in other ModelView tutorials, Qt's MVC may not be the same as the conventional MVC.

If the view and the controller objects are combined, the result is the model/view architecture. This still separates the way that data is stored from the way that it is presented to the user, but provides a simpler framework based on the same principles. This separation makes it possible to display the same data in several different views, and to implement new types of views, without changing the underlying data structures. To allow flexible handling of user input, we introduce the concept of the delegate. The advantage of having a delegate in this framework is that it allows the way items of data are rendered and edited to be customized.
However, it's worth investigating their approach of using "delegate" with their Model/View pattern.


InitialProjectFileSystemModel.png  

In this tutorial, we'll use Model-Based TreeView for folders on the left side and ListView for files on the right side:

QFileSystemModelLayout.png  

Since we finished layout, now is the time for coding.

Let's make two models in qfilesystemmodeldialog.h: one for files and the other for directories. We make two models because we need to filter them separately.

#ifndef QFILESYSTEMMODELDIALOG_H
#define QFILESYSTEMMODELDIALOG_H

#include <QDialog>
#include <QFileSystemModel>

namespace Ui {
class QFileSystemModelDialog;
}

class QFileSystemModelDialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit QFileSystemModelDialog(QWidget *parent = 0);
    ~QFileSystemModelDialog();
    
private:
    Ui::QFileSystemModelDialog *ui;

    // Make two models instead of one
    // to filter them separately
    QFileSystemModel *dirModel;
    QFileSystemModel *fileModel;
};

#endif // QFILESYSTEMMODELDIALOG_H

Move on to the implementation file, qfilesystemmodeldialog.cpp:

#include "qfilesystemmodeldialog.h"
#include "ui_qfilesystemmodeldialog.h"

QFileSystemModelDialog::QFileSystemModelDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QFileSystemModelDialog)
{
    ui->setupUi(this);

    // Creates our new model and populate
    QString mPath = "C:/";
    dirModel = new QFileSystemModel(this);

    // Set filter
    dirModel->setFilter(QDir::NoDotAndDotDot |
                        QDir::AllDirs);

    // QFileSystemModel requires root path
    dirModel->setRootPath(mPath);

    // Attach the model to the view
    ui->treeView->setModel(dirModel);
}

QFileSystemModelDialog::~QFileSystemModelDialog()
{
    delete ui;
}

Let's run the code to see what we've done.

QFileSystemModelRunA.png  

It shows the TreeView for directories successfully, and it looks like we're on track!

How about files?

We add similar line of code for files in the implementation file, qfilesystemmodeldialog.cpp:

    // FILES

    fileModel = new QFileSystemModel(this);

    // Set filter
    fileModel->setFilter(QDir::NoDotAndDotDot |
                        QDir::Files);

    // QFileSystemModel requires root path
    fileModel->setRootPath(mPath);

    // Attach the model to the view
    ui->listView->setModel(fileModel);

Let's setup a slot for the TreeView by "Go to slot..."->clicked(ModelIndex).

GoToSlotTreeView.png  

void QFileSystemModelDialog::on_treeView_clicked(const QModelIndex &index)
{
    // TreeView clicked
    // 1. We need to extract path
    // 2. Set that path into our ListView

    // Get the full path of the item that's user clicked on
    QString mPath = dirModel->fileInfo(index).absoluteFilePath();
    ui->listView->setRootIndex(fileModel->setRootPath(mPath));

}

Again, let's run the code:

QFileSystemModelRunFinal.png  

Great!



Here are the final codes:

qfilesystemmodeldialog.h:

#ifndef QFILESYSTEMMODELDIALOG_H
#define QFILESYSTEMMODELDIALOG_H

#include <QDialog>
#include <QFileSystemModel>

namespace Ui {
class QFileSystemModelDialog;
}

class QFileSystemModelDialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit QFileSystemModelDialog(QWidget *parent = 0);
    ~QFileSystemModelDialog();
    
private slots:
    void on_treeView_clicked(const QModelIndex &index);

private:
    Ui::QFileSystemModelDialog *ui;

    // Make two models instead of one
    // to filter them separately
    QFileSystemModel *dirModel;
    QFileSystemModel *fileModel;
};

#endif // QFILESYSTEMMODELDIALOG_H


The implementation file, qfilesystemmodeldialog.cpp:

#include "qfilesystemmodeldialog.h"
#include "ui_qfilesystemmodeldialog.h"

QFileSystemModelDialog::QFileSystemModelDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QFileSystemModelDialog)
{
    ui->setupUi(this);

    // Creates our new model and populate
    QString mPath = "C:/";

    // DIRECTORIES

    dirModel = new QFileSystemModel(this);

    // Set filter
    dirModel->setFilter(QDir::NoDotAndDotDot |
                        QDir::AllDirs);

    // QFileSystemModel requires root path
    dirModel->setRootPath(mPath);

    // Attach the dir model to the view
    ui->treeView->setModel(dirModel);


    // FILES

    fileModel = new QFileSystemModel(this);

    // Set filter
    fileModel->setFilter(QDir::NoDotAndDotDot |
                        QDir::Files);

    // QFileSystemModel requires root path
    fileModel->setRootPath(mPath);

    // Attach the file model to the view
    ui->listView->setModel(fileModel);
}

QFileSystemModelDialog::~QFileSystemModelDialog()
{
    delete ui;
}

void QFileSystemModelDialog::on_treeView_clicked(const QModelIndex &index)
{
    // TreeView clicked
    // 1. We need to extract path
    // 2. Set that path into our ListView

    // Get the full path of the item that's user clicked on
    QString mPath = dirModel->fileInfo(index).absoluteFilePath();
    ui->listView->setRootIndex(fileModel->setRootPath(mPath));

}


Qt进阶开发:QFileSystemModel的使用 本文主要介绍模型/视图架构中QFileSystemModel的使用。 阅读详情

相关推荐

Qt QTreeView QFileSystemModel功能及用法详解

QFileSystemModel提供了一个可用于访问本机文件系统的数据模型。 QFileSystemModel 和视图组件 QTreeView 结合使用,可以用目录树的形式显示本机上的文件系统,如同 Widnows 的资源管理器一样。使用 QFileSystemModel 提供的接口函数,可以创建目录、删除目录、重命名目录,可以获得文件名称、目录名称、文件大小等参数,还可以获得文件的详细信息。 ...

weixin_38293453的博客 3303

QFileSystemModel+QTableView显示自定义图标icon

平台:QT5.12.9 + Windows + mingw32(可直接编译,默认是桌面路径) 实现QFileSystemModel+QTableView创建文件管理系统,基于windows平台。 进一步优化该功能,为了适配嵌入式arm平台,实现QFileSystemModel自定义修改图标功能。这样用户可以通过不同文件类型需求,自定义自己所要显示的图标。 说明:该程序默认文件路径为桌面路径,测试自定义的文件后缀类型为.txt,如果需要改为其他类型,仿照txt部分进行实现

qtQFileSystemModel和QStringListModel使用

/把树形视图的点击信号和表格视图的设置开始节点槽函数关联起来,达到在树形视图里每次点击,就在表格视图里显示对应的文件列表的目的。//再修改节点对象的值,传递上边的节点对象,要修改的值。...

1038

QFileSystemModel替换系统图标

QFileSystemModel替换系统图标

QT => QFileSystemModel+QTableView显示自定义图标icon

在项目中需要使用到文件管理系统,所以用到了QT中的QFileSystemModel+QTableView部分。对于windows平台来说,不同文件类型有不同的文件图标显示,较为友好完善,但是在嵌入式ARM平台就没有那么完全,一般都会自定义所需文件类型的图标显示,因此本文讲述了如何改变QFileSystemModel索引默认平台图标,修改为自定义icon方式。 二、使用步骤 目前似乎只有这一种方法。 首先要自定义类,继承QFileSystemModel,然后重写data函数 .h文件代码如下(示例): .cp

ZML的博客 1574

QT通过QFileSystemModel自定义文件夹浏览器

在嵌入式的7寸的触摸屏上调用系统的资源管理器浏览文件,字体太小不方便客户的操作,于是就通过QFileSystemModel可以自定义文件夹浏览器,经过几天摸索终于完成

liang520999的博客 1078

QTreeView使用总结13,自定义model示例,大大优化性能和内存

前面简单介绍过Qt的模型/视图框架,提到了Qt预定义的几个model类型:QStringListModel:存储简单的字符串列表QStandardItemModel:可以用于树结构的存储,提供了层次数据QFileSystemModel:本地系统的文件和目录信息QSqlQueryModel、QSqlTableModel、QSqlRelationalTableModel:存取数据库数据。

逆枫 -- C++/Qt工程师、创业者 2万+

QT使用QFileSystemModel实现的文件资源管理器(开源)

文件资源管理器:支持文件/文件夹拖拽,复制,粘贴,剪切,删除,重命名的基本操作,支持打开图片,文档等资源,支持文件显示详细信息,支持文件路径导航

Ray 4993

QTQFileSystemModel类的应用介绍

本文是QFileSystemModel类的应用介绍,QFileSystemModel提供了一个可用于访问本机文件系统的数据模型。QFileSystemModel和视图组件QTreeView结合使用,可以用目录树的形式显示本机上的文件系统,如同Widnows的资源管理器一样。使用QFileSystemModeI提供的接口函数,可以创建目录、删除目录、重命名目录,可以获得文件名称、目录名称、文件大小等参数,还可以获得文件的详细信息。

u011671745的博客 1586

QtQFileSystemModel 使用记录

QFileSystemModel类:Qt帮助中的介绍为: The QFileSystemModel class provides a data model for the local filesystem. This class provides access to the local filesystem, providing functions for renaming and removi...

Linux 2144

Qt QFileSystemModel类详解

Qt QFileSystemModel类详解

qq_30150579的博客 1492

Qt】之 QTreeView和QFileSystemModel

Qt来显示一个文件目录是很简单的,如下: QFileSystemModel *model = new QFileSystemModel(); model->setRootPath("/"); //model->setFilter(QDir::Dirs|QDir::NoDotAndDotDot); //只显示文件夹 // 设置过滤器 QString

Teng's world 1万+

qt QFileSystemModel详解

QFileSystemModelQt框架中的一个关键类,它继承自QAbstractItemModel,专门用于在Qt应用程序中展示文件系统的数据。这个模型提供了一个方便的接口,使得开发者可以轻松地在应用程序中集成文件和目录的树形结构,并通过视图组件(如QTreeView、QListView等)展示给用户。QFileSystemModel与操作系统文件系统交互,将文件和目录的层次结构转换为数据模型,从而实现了文件系统的可视化。

ckg3824278的博客 1592

QT6(QFileSystemModelQTreeView)

QT6文件系统模型与视图组件 本文介绍了QT6中QFileSystemModelQTreeView的配合使用,用于实现类似Windows资源管理器的文件系统浏览功能。QFileSystemModel提供了丰富的接口,支持目录创建/删除/重命名,以及获取文件属性和详细信息。 文章详细列出了QTreeView的常用属性和方法,包括模型管理、外观布局、展开折叠、选择操作、编辑功能等类别,并附有示例代码展示如何构建一个完整的文件浏览器界面,实现多视图同步显示和文件过滤功能。

weixin_43754657的博客 905

Qt QFileSystemModel详解

QFileSystemModel QFileSystemModel提供了一个可用于访问本机文件系统的数据模型。 QFileSystemModel 和视图组件 QTreeView 结合使用,可以用目录树的形式显示本机上的文件系统,如同 Widnows 的资源管理器一样。使用 QFileSystemModel 提供的接口函数,可以创建目录、删除目录、重命名目录,可以获得文件名称、目录名称、文件大小等参数,还可以获得文件的详细信息。 要通过 QFileSystemModel 获得本机的文件系统,需要用 se.

希望能帮助大家,希望大家多多支持,你们的支持是我前进的动力。 7426

QTQFileSystemModel类的使用

详细说明 QFileSystemModel类为本地文件系统提供数据模型。 此类提供对本地文件系统的访问,提供了用于重命名和删除文件和目录以及创建新目录的功能。在最简单的情况下,它可以与适当的显示小部件一起使用,作为浏览器或过滤器的一部分。 可以使用QAbstractItemModel提供的标准接口访问QFileSystemModel,但是它还提供了一些特定于目录模型的便捷功能。 fileInfo(),isDir(),fileName()和filePath()函数提供有关与模型中的项目相关的基础文件和目录的信

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1113

Qt数据模型 - QFileSystemModel使用教程

QFileSystemModel类是Qt中一个非常有用的数据模型,可用于在本地文件系统中显示目录结构。在使用时,我们只需要设置根路径并将其与QTreeView一起使用即可达到显示目录结构的效果。通过对该类的深入研究,可以了解一些更高级的用法,并利用其在图形化编程中提供的优势。应替换为指向要显示的目录的路径。这将设置QFileSystemModel的根路径,并使其准备好显示该目录中的文件和子目录。在上面的示例代码中,“/path/to/directory”应替换为要显示的目录的路径。

. 1108

QT 数据模型结构学习 :QFileSystemModel

qt自带几个比较方便的数据结构,今天学习 继承自 QAbstractItemModel 的 QFileSystemModelQFileSystemModel是一个用于访问本机文件系统的数据模型(还有一个是QDirModel,两者主要区别是使用主线程还是独立线程,建议使用独立线程的QFileSystemModel)。QFileSystemModel配合qt的view可以实现目录的访问。 QF...

Beyond欣 2219
上一篇: Qt 之 QWebView
下一篇: qt 之好资料
icatchyou
博客等级 码龄14年 6粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值