一.定义
二.功能
三.代码示例
#include <QApplication>
#include <QTreeView>
#include <QFileSystemModel>
#include <QDir>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建一个QFileSystemModel对象
QFileSystemModel model;
// 设置文件系统的根路径为当前工作目录
QString rootPath = QDir::currentPath();
model.setRootPath(rootPath);
// 创建一个QTreeView对象,并将QFileSystemModel设置为其模型
QTreeView treeView;
treeView.setModel(&model);
// 设置QTreeView的根索引为模型的根目录索引
QModelIndex rootIndex = model.index(rootPath);
treeView.setRootIndex(rootIndex);
// 打印根路径下的子文件和子文件夹名
int rowCount = model.rowCount(rootIndex);
for (int i = 0; i < rowCount; ++i) {
QModelIndex childIndex = model.index(i, 0, rootIndex);
QString childName = model.fileName(childIndex);
qDebug() << "Child Name:" << childName;
}
treeView.setWindowTitle("File System Viewer");
treeView.show();
return a.exec();
}
四.模型索引介绍