1. 程式人生 > >QT 實現 讀取 增加 刪除 實時操作xml

QT 實現 讀取 增加 刪除 實時操作xml

include "operatexml.h"

include

OperateXml::OperateXml(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
readXML();
load();
connect(ui.pushButton_add, &QPushButton::clicked, this, &OperateXml::add);
connect(ui.pushButton_del, &QPushButton::clicked, this, &OperateXml::del);
connect(ui.tableView, SIGNAL(clicked(const QModelIndex &)), this,
SLOT(selectItem(const QModelIndex &)));
}

OperateXml::~OperateXml()
{

}
void OperateXml::selectItem(const QModelIndex &)
{

}
void OperateXml::add()
{
cityInfo city;
city.name = ui.lineEdit_name->text();
bool* lonOK = new bool;
bool* latOK=new bool;
QString lon = ui.lineEdit_lon->text();
city.lon = lon.toDouble(lonOK);
QString lat = ui.lineEdit_lon->text();
city.lat = lat.toDouble(latOK);
if (city.name.length() > 15)
{
QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("名稱長度超過15個字元")); return;
}
if (city.name.trimmed().length() ==0)
{
QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("名稱不能為空")); return;
}
if (lonOK == false)
{
QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("經度為非數字")); return;
}
if (

latOK == false)
{
QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("緯度為非數字")); return;
}
m_cityList.push_back(city);
saveXML();
ui.lineEdit_name->clear();
ui.lineEdit_lon->clear();
ui.lineEdit_lat->clear();
load();
}
void OperateXml::del()
{
int num=ui.tableView->currentIndex().row();
//m_model->removeRow(num);
//獲取選中行名稱
QModelIndex index = m_model->index(num, 0);
QString name = m_model->data(index).toString();
std::vector

void OperateXml::load()
{

m_model = new QStandardItemModel();
m_model->setColumnCount(3);
m_model->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit("名稱"));
m_model->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit("經度"));
m_model->setHeaderData(2, Qt::Horizontal, QString::fromLocal8Bit("緯度"));
if (!m_cityList.empty())
{
    for (int i = 0; i < m_cityList.size(); i++)
    {
        m_model->setItem(i, 0, new QStandardItem(m_cityList[i].name));
        m_model->setItem(i, 1, new QStandardItem(QString::number(m_cityList[i].lon)));
        m_model->setItem(i, 2, new QStandardItem(QString::number(m_cityList[i].lat)));
    }
}
ui.tableView->setModel(m_model);
ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

}
void OperateXml::readXML()
{
//開啟或建立檔案
QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都行
if (!file.open(QFile::ReadOnly))
return;

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

QDomElement root = doc.documentElement(); //返回根節點
QDomNode node = root.firstChild(); //獲得第一個子節點
while (!node.isNull())  //如果節點不空
{
    if (node.isElement()) //如果節點是元素
    {
        cityInfo  city;
        QDomElement e = node.toElement(); //轉換為元素,注意元素和節點是兩個資料結構,其實差不多
        if (e.tagName() == "city")
        {
            city.name = e.attribute("name");
            city.lon = e.attribute("lon").toDouble();
            city.lat = e.attribute("lat").toDouble();
            m_cityList.push_back(city);
        }
    }
    node = node.nextSibling(); //下一個兄弟節點,nextSiblingElement()是下一個兄弟元素,都差不多
}

}
void OperateXml::saveXML()
{
//開啟或建立檔案
QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都可以
if (!file.open(QFile::WriteOnly | QFile::Truncate)) //可以用QIODevice,Truncate表示清空原來的內容
return;

QDomDocument doc;
//寫入xml頭部
QDomProcessingInstruction instruction; //新增處理命令
instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
//新增根節點
QDomElement root = doc.createElement("lib");
doc.appendChild(root);
int n = m_model->rowCount();
for (int i = 0; i < m_cityList.size(); i++)
{
    //新增一個子節點及其子元素
    QDomElement city = doc.createElement("city");
    /*QModelIndex indexName = m_model->index(i, 0);
    QModelIndex indexLon = m_model->index(i, 1);
    QModelIndex indexLat = m_model->index(i, 2);
    QString name = m_model->data(indexName).toString();
    QString lon = m_model->data(indexLon).toString();
    QString lat = m_model->data(indexLat).toString();*/

    city.setAttribute("name",m_cityList[i].name); //方式一:建立屬性  其中鍵值對的值可以是各種型別
    city.setAttribute("lon", m_cityList[i].lon); //方式一:建立屬性  其中鍵值對的值可以是各種型別
    city.setAttribute("lat", m_cityList[i].lat); //方式一:建立屬性  其中鍵值對的值可以是各種型別
    root.appendChild(city);
}
//輸出到檔案
QTextStream out_stream(&file);
doc.save(out_stream, 4); //縮排4格
file.close();

}