Qt 模型视图详细介绍

一.文件系统模型(QFileSystemModel)

1.定义

QFileSystemModel 是 Qt 框架中的一个类,它提供了一个用于管理文件系统结构的模型。它可以用于显示文件系统的目录结构,以及在视图中显示文件和文件夹的详细信息。

这个模型将文件系统表示为一个树形结构,其中根节点代表文件系统的根目录。它可以方便地与 QTreeView 或 QListView 等视图部件一起使用,从而在 GUI 应用程序中显示文件和文件夹。QFileSystemModel 还具有内置的排序和筛选功能,以便根据特定的规则显示文件和文件夹。

 2.常用方法

QFileSystemModel 类提供了许多功能方法用于管理文件系统的结构和文件/文件夹的信息。以下是一些常用的方法:

  1. setRootPath(const QString &path):设置根路径,指定文件系统模型应该从哪个路径开始显示文件和文件夹。

  2. index(int row, int column, const QModelIndex &parent = QModelIndex()):返回给定行和列的模型索引,可以用来访问特定位置的文件或文件夹。

  3. data(const QModelIndex &index, int role = Qt::DisplayRole):返回指定索引处的数据,根据给定的 role 返回不同类型的数据,比如文件名、文件大小等。

  4. fileName(const QModelIndex &index)filePath(const QModelIndex &index)fileInfo(const QModelIndex &index):分别返回指定索引处的文件名、文件路径和文件信息(例如文件大小、修改时间等)。

  5. rowCount(const QModelIndex &parent = QModelIndex()):返回指定父索引下的行数,用于确定文件夹内文件/文件夹的数量。

  6. columnCount(const QModelIndex &parent = QModelIndex()):返回列数,用于确定显示的列数。

  7. flags(const QModelIndex &index):返回指定索引处的项支持的操作,比如是否可编辑、是否可选择等。

  8. canFetchMore(const QModelIndex &parent):检查是否可以加载更多的数据。

  9. hasChildren(const QModelIndex &parent = QModelIndex()):检查指定索引是否有子项。

这些方法使得 QFileSystemModel 可以方便地管理文件系统的结构,并提供了访问文件和文件夹信息的功能。通过这些方法,可以轻松地在 GUI 应用程序中显示文件系统内容,并对其进行操作。

3.示例 

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

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

    QFileSystemModel model;
    model.setRootPath(QDir::currentPath());

    QTreeView treeView;
    treeView.setModel(&model);
    treeView.setRootIndex(model.index(QDir::currentPath()));

    treeView.show();

    return app.exec();
}

4.TreeView介绍

1.首先,创建一个 QFileSystemModel 对象并设置根路径:

QFileSystemModel model;
model.setRootPath(QDir::currentPath());

2.创建一个 QTreeView 对象并将 QFileSystemModel 设置为其模型:

QTreeView treeView;
treeView.setModel(&model);

3.设置 QTreeView 的根索引为根路径的索引,以显示文件系统的结构:

treeView.setRootIndex(model.index(QDir::currentPath()));

5.ListView介绍

1.类似地,创建一个 QFileSystemModel 对象并设置根路径:

QFileSystemModel model;
model.setRootPath(QDir::currentPath());

 2.创建一个 QListView 对象并将 QFileSystemModel 设置为其模型:

QListView listView;
listView.setModel(&model);

 3.设置 QListView 的显示模式为列表模式:

listView.setViewMode(QListView::ListMode);

 二.字符串链表模型(QStringListModel)

1.定义

QStringListModel 是 Qt 框架提供的一个方便的模型类,用于在 Qt 应用程序中存储和展示字符串数据。它是 QAbstractListModel 的子类,可以用于在 Qt 的部件中显示字符串列表,例如 QListViewQComboBoxQTableView 等。

2.创建和初始化 QStringListModel 

1.首先,创建一个 QStringListModel 对象并初始化一个字符串列表:

QStringList stringList;
stringList << "Apple" << "Banana" << "Cherry" << "Durian";

QStringListModel model(stringList);

2.可以直接在构造函数中传入字符串列表,也可以在创建后使用 setStringList() 方法设置字符串列表:

model.setStringList(stringList);

 3.在部件中展示 QStringListModel 中的数据

1.创建一个 QListView 部件,并将 QStringListModel 设置为其模型:

QListView listView;
listView.setModel(&model);

 

2.如果需要在 QComboBox 中显示字符串列表,也可以使用 QStringListModel

QComboBox comboBox;
comboBox.setModel(&model);

4.示例

#include <QApplication>
#include <QListView>
#include <QStringListModel>

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

    QStringList stringList;
    stringList << "Apple" << "Banana" << "Cherry" << "Durian";

    QStringListModel model(stringList);

    QListView listView;
    listView.setModel(&model);
    listView.show();

    return app.exec();
}

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值