0
点赞
收藏
分享

微信扫一扫

Qt 文件模型(QFileSystemModel)详细介绍

谷中百合517 2024-05-31 阅读 11

一.定义

二.功能

三.代码示例

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

四.模型索引介绍

举报

相关推荐

0 条评论