-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkConfigWidget.cpp
90 lines (73 loc) · 2.34 KB
/
NetworkConfigWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "NetworkConfigWidget.h"
#include "ui_NetworkConfigWidget.h"
#include <QtXml>
#include <QFile>
#include <QDebug>
#include <map>
struct NetworkConfigWidgetPrivate
{
QDomDocument doc;
std::map<QLineEdit*,QDomElement> NodeMap;
};
NetworkConfigWidget::NetworkConfigWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::NetworkConfigWidget),p(new NetworkConfigWidgetPrivate)
{
ui->setupUi(this);
QFile file("C:\\Users\\86151\\Desktop\\test.xml"); //本地配置xml文件
if(file.open(QIODevice::ReadOnly) == false)
{
qDebug()<<"文件打开失败!\n";
return;
}
auto Content = file.readAll();
file.close();
//xml文本对象
QString errorString;
int row,column;
if(p->doc.setContent(Content,&errorString,&row,&column) == false)
{
qDebug()<<"xml格式错误:"<<errorString<<":"<<row<<":"<<column;
return;
}
qDebug()<<"xml读取成功!\n";
//xml根节点元素
QDomElement rootNode = p->doc.documentElement();
qDebug()<<"根节点名称:"<<rootNode.nodeName()<<endl;
ui->lineEdit->setText(rootNode.firstChildElement("ip").text());
ui->lineEdit_2->setText(rootNode.firstChildElement("port").text());
p->NodeMap[ui->lineEdit] = rootNode.firstChildElement("ip");
p->NodeMap[ui->lineEdit_2] = rootNode.firstChildElement("port");
connect(ui->QPushButton_ok,&QPushButton::clicked,this,
&NetworkConfigWidget::PushButton_ok_clicked);
}
NetworkConfigWidget::~NetworkConfigWidget()
{
delete ui;
delete p;
}
QString NetworkConfigWidget::GetIP()
{
return ui->lineEdit->text();
}
int NetworkConfigWidget::GetPort()
{
return ui->lineEdit_2->text().toUShort();
}
void NetworkConfigWidget::PushButton_ok_clicked()
{
auto ipNode = p->NodeMap.find(ui->lineEdit)->second;
ipNode.firstChild().setNodeValue(ui->lineEdit->text());
auto portNode = p->NodeMap.find(ui->lineEdit_2)->second;
portNode.firstChild().setNodeValue(ui->lineEdit_2->text());
QFile file("C:\\Users\\86151\\Desktop\\test.xml");
if(file.open(QIODevice::WriteOnly) == false)
{
qDebug()<<"文件打开失败!\n";
return;
}
file.write(p->doc.toByteArray());
file.close();
this->close();
emit PBOK_Press();
}