QListWidget读取本地文件夹中文件并显示名字,双击读取xml数据

本文介绍了如何使用QListWidget在Qt应用中读取本地文件夹中的xml文件,并展示文件名,以及双击事件触发时解析文件内容的操作。重点讲解了读取文件路径获取、文件名遍历和XML数据的DOM解析技术。
摘要由CSDN通过智能技术生成


前言

本文主要是为了解决QListWidget读取本地文件夹中文件,并且显示的问题。然后双击读取其中xml数据


一、读取本地文件并显示

如下的函数是用来读取本地文件夹中文件,并读取文件的名字。

QStringList Sensor::getFileNames(const QString &path)
{
    QDir dir(path);
    QStringList nameFilters;
    nameFilters <<"*.xml";   //在这换想要读取的文件的文件格式,可以写多个格式
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    return files;
}

首先这个是我的文件路径,现在调试放在debug中,如果打包成exe直接放在exe根目录
在这里插入图片描述所以我的路径使用这样获取

QApplication::applicationDirPath() + "/xml/Sensor"

使用如下

void Sensor::readfile()
{
    qDebug() << QApplication::applicationDirPath() + "/xml/Sensor";
    QStringList fileName = getFileNames(QApplication::applicationDirPath() + "/xml/Sensor");
    qDebug() << fileName;
    ui->listWidget_xml->clear();  //先清除上一次的数据
    for (int i=0;i<fileName.size();i++) {
        ui->listWidget_xml->addItem(fileName[i]);  //把读取到的数据写入到QListWidget中
    }

}

二、双击读取数据

第一步选择双击的槽函数
在这里插入图片描述

槽函数代码如下(示例):

void Sensor::on_listWidget_xml_doubleClicked(const QModelIndex &index)
{
   qDebug()<< QApplication::applicationDirPath() + "/xml/Sensor";

   QString Path = QApplication::applicationDirPath() + "/xml/Sensor";
   QString file =  ui->listWidget_xml->currentItem()->text();  //获取到item的数据

   Path.append("/"+file);

   QStringList sensor = GetStringConfigValue(Path);

   qDebug()<<sensor;
//   ui->tabWidg_PF;
}
//获取字符串字段
QStringList Sensor::GetStringConfigValue(QString str) {

    if(str == "InitDeviceNo")
    {
//        return getMacPath();
    }else
    {
        QString filePath= str;
        QStringList names;
        打开或创建文件
        QFile file(filePath); //相对路径、绝对路径、资源路径都行
        if(!file.open(QFile::ReadOnly))
        {

        }
        QDomDocument doc;
        if(!doc.setContent(&file))
        {
            file.close();

        }
        file.close();
        QDomElement root=doc.documentElement(); //返回根节点
        QDomNode node=root.firstChild(); //获得第一个子节点

        while(!node.isNull())  //如果节点不空
        {
            if(node.isElement()) //如果节点是元素
            {
                QDomElement domElement =node.toElement(); //转换为元素,注意元素和节点是两个数据结构,其实差不多

                qDebug() << domElement.nodeName() << ":" << domElement.toElement().attribute("id")<<  domElement.toElement().attribute("naming");

                QString id = domElement.toElement().attribute("id");
                QString naming = domElement.toElement().attribute("naming");
                names.append(id);
                names.append(naming);
            }
            node=node.nextSibling(); //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素,都差不多
        }
        return names;

    }
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值