// 将销售记录写入文档 void Widget::writeXml() { // 先从文件读取 if (docRead()) { QString currentDate = getDateTime(Date); QDomElement root = doc.documentElement(); // 根据是否有日期节点进行处理 if (!root.hasChildNodes()) { QDomElement date = doc.createElement(QString("日期")); QDomAttr curDate = doc.createAttribute("date"); curDate.setValue(currentDate); date.setAttributeNode(curDate); root.appendChild(date); createNodes(date); } else { QDomElement date = root.lastChild().toElement(); // 根据是否已经有今天的日期节点进行处理 if (date.attribute("date") == currentDate) { createNodes(date); } else { QDomElement date = doc.createElement(QString("日期")); QDomAttr curDate = doc.createAttribute("date"); curDate.setValue(currentDate); date.setAttributeNode(curDate); root.appendChild(date); createNodes(date); } } // 写入到文件 docWrite(); } }
//显示日销售清单 // 显示日销售清单 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("日期:%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 type = list.at(0).toElement().text(); QString brand = 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 + " 出售 " + brand + type + " " + num + "台, " + "单价:" + price + "元, 共" + sum + "元"; QListWidgetItem *tempItem = new QListWidgetItem; tempItem->setText("**************************"); tempItem->setTextAlignment(Qt::AlignCenter); ui->dailyList->addItem(tempItem); ui->dailyList->addItem(str); } } } } }