【QT】 QTreeView/QTreeWidget插入文件目录列表

目录

1 QTreeView插入文件目录列表

1.1 自定义默认展开指定路径及文件

1.2 展开指定路径的所有目录及文件

  2 QTreeWidget插入文件目录列表

QTreeView插入文件目录列表

  显示指定磁盘下的目录,简单的方式就是利用QTreeView+QDirModel就可以显示了。

1.1 自定义默认展开指定路径及文件

 
   //获取程序所在路径
    QString applicationDirPath = QCoreApplication::applicationDirPath();
    qDebug() << "applicationDirPath=" << applicationDirPath;
    QString dirPath = applicationDirPath + "/slMap/安徽省";
    QDirModel *dirMode = new QDirModel;
    ui->treeView->setModel(dirMode);
    ui->treeView->setRootIndex(dirMode->index(dirPath));
    ui->treeView->allColumnsShowFocus();
    
    //需要默认展开的文件位置
    QString filepath=dirPath+ "/合肥市/滨湖区/滨湖考场/综合类考场";
    QStringList list = filepath.split("/");
    //每次循环需要打开的文件路径
    QString findpath;
    
    //这里不能直接使用ui->treeView->setExpanded(model->index(目标文件路径),1);
    //这样只能打开第一层文件夹,因此需要使用循环一层一层的打开。
    
    //循环一层一层的打开文件夹直到目标文件夹被打开
    foreach(QString addstr,list)
    {
        if(findpath.size()>0)
        {
            addstr='/'+addstr;
        }
        findpath+=addstr;
        ui->treeView->setExpanded(dirMode->index(findpath),1);
    }
这里默认展开“D:\...\slMap\安徽省\合肥市\滨湖区\滨湖考场\综合类考场”路径。
注意:不能直接使用 ui->treeView->setExpanded(model->index(目标文件路径),1), 这样只能打开第一层文件夹,setExpanded()只能打开当前文件下的文件夹要使得指定路径被打开,只能一层一层的打开文件,直到目标文件夹被打开。

1.2 展开指定路径的所有目录及文件

//获取程序所在路径
    QString applicationDirPath = QCoreApplication::applicationDirPath();
    qDebug() << "applicationDirPath=" << applicationDirPath;
    QString dirPath = applicationDirPath + "/slMap/安徽省";
    QDirModel *dirMode = new QDirModel;
    ui->treeView->setModel(dirMode);
    ui->treeView->setRootIndex(dirMode->index(dirPath));
    ui->treeView->allColumnsShowFocus();
    // 设置是否展开所有目录,当目录过多时候 设置展开容易卡住
    ui->treeView->expandAll();
这里展开“D:\...\slMap\安徽省“路径。

  2 QTreeWidget插入文件目录列表

    先遍历指定路径下的目录文件夹名称,再插入 QTreeWidget控件。
void MainWindow::initTreeWidget()
{
    //获取程序所在路径
    QString applicationDirPath = QCoreApplication::applicationDirPath();
    qDebug() << "applicationDirPath=" << applicationDirPath;
    dirPath = applicationDirPath + "/slMap/安徽省";
    QStringList dirNames = getDirNames(dirPath);

    ui->treeWidget->setColumnCount(2);
    ui->treeWidget->setHeaderLabels(QStringList()<<"名称"<<"描述");

    for(int i=0;i<dirNames.size();i++)
    {
        QTreeWidgetItem * item = AddTreeRoot(dirNames.at(i),"市区");
        QString subdirPath = dirPath + "/" +dirNames.at(i);
            getSubDir(item,subdirPath);
    }

    ui->treeWidget->expandAll();
}
QStringList MainWindow::getDirNames(QString dirPath)
{
    QDir dir(dirPath);
    //--1 判断文件夹是否存在
    if(!dir.exists())
    {
        CustomMessagebox msgBox(MessageType::MT_Error, "文件夹找不到", this);
    }
    else{
        //--2 获取当前路径下所有的文件夹名字
        QStringList names = dir.entryList(QDir::Dirs);

        //--3 删除当前文件夹和上级文件夹(温馨提示:隐藏的文件夹获取不了)
        names.removeOne(".");
        names.removeOne("..");

        //--4 打印出获取的文件名
        qDebug() << "names: " << names;
        return names;
    }
}
void MainWindow::getSubDir(QTreeWidgetItem *item,QString subDirPath)
{
    // 创建 QDirIterator 对象并指定根目录路径、过滤器等参数
    QDirIterator it(subDirPath, QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::NoIteratorFlags);
    while (it.hasNext()) {
        // 获取当前文件或目录的路径
        QString fileOrDirectoryPath = it.next();
        qDebug() << "File or directory path:" << fileOrDirectoryPath;
        QTreeWidgetItem * subitem = AddTreeNode(item,it.fileName(),"区/县");
    }
}
QTreeWidgetItem * MainWindow::AddTreeRoot(QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    ui->treeWidget->addTopLevelItem(item);
    return item;
}

QTreeWidgetItem * MainWindow::AddTreeNode(QTreeWidgetItem *parent,QString name,QString desc)
{
    QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<name<<desc);
    parent->addChild(item);
    return item;
}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷凝女子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值