2 xunyou6014 xunyou6014 于 2017.09.15 00:14 提问

qt xml创建、读取、显示相关问题

第一次接触qt和xml,依照qt开发及实例改变了代码,用qt创建了一个xml文档但是在工程目录中没有显示xml文档,把xml文档读取出来也没有再ui上显示,问问大佬们是什么原因??

创建xml文档代码

 static bool createXml()
{
//    qDebug()<<"QQAA";
    QFile file("data.xml");
    if(file.exists())return true;       //为不重复创建该文件,首先判断是否存在
//    qDebug()<<"SSDD";
    if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))
        return false;
    QDomDocument doc;
    QDomProcessingInstruction instruction;
    instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
    QDomElement root = doc.createElement(QString("daily sell list"));
    doc.appendChild(root);
    QTextStream out(&file);
    doc.save(out,4);
    file.close();
//    qDebug()<<"XXXX";
    return true;
}
 bool Widget::docRead()
{

    QFile file("data.xml");
    if(!file.open(QIODevice::ReadOnly))
        return false;
    if(!doc.setContent(&file)){  //此句有问题直接return false
        file.close();
        return false;
    }
    file.close();
    return true;
}

显示数据

 void Widget::showDailyList()
{
    ui->dailyList->clear();
    if(docRead()){
        QDomElement root = doc.documentElement();
        QString title = root.tagName();
        QListWidgetItem *titleItem = new QListWidgetItem;
        titleItem->setText(QString("-----%1-----").arg(title));
        titleItem->setTextAlignment(Qt::AlignCenter);
        ui->dailyList->addItem(titleItem);
        if(root.hasChildNodes()){
            QString currentDate = getDateTime(Date);
            QDomElement dateElement = root.lastChild().toElement();
            QString date = dateElement.attribute("date");
            if(date == currentDate){
                ui->dailyList->addItem("");
                ui->dailyList->addItem(QString("buyDate:%1").arg(date));
                ui->dailyList->addItem("");
                QDomNodeList children = dateElement.childNodes();
                //遍历当日销售的所有汽车
                for(int i=0;i<children.count();i++){
                    QDomNode node=children.at(i);
                    QString time=node.toElement().attribute("time");
                    QDomNodeList list=node.childNodes();
                    QString house=list.at(0).toElement().text();
                    QString bookname=list.at(1).toElement().text();
                    QString price=list.at(2).toElement().text();
                    QString num=list.at(3).toElement().text();
                    QString sum=list.at(4).toElement().text();
                    QString str=time+"sell"+bookname+house+" "+num+"book, "+"unitprice:"+price+"yuan,in total "+sum+"yuan";
                    QListWidgetItem *tempItem=new QListWidgetItem;
                    tempItem->setText("**************************");
                    tempItem->setTextAlignment(Qt::AlignCenter);
                    ui->dailyList->addItem(tempItem);
                    ui->dailyList->addItem(str);
                }
            }
        }
    }
    /*
   else
    {
        QListWidgetItem *titleItem = new QListWidgetItem;
        titleItem->setText(QString("-----************************-----"));
        ui->dailyList->addItem(titleItem);
    }
    */
}

1个回答

caozhy
caozhy   Ds   Rxr 2017.09.15 08:55
Csdn user default icon
上传中...
上传图片
插入图片