-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
125 lines (116 loc) · 4.23 KB
/
Copy pathmainwindow.cpp
File metadata and controls
125 lines (116 loc) · 4.23 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cstdlib>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <QJsonObject>
#include <QTimer>
//название жисона из которого читаем
QString FILE_NAME = "test.json";
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableDevicesWidget->setColumnCount(3);
QStringList headers;
headers.append("Название");
headers.append("Серийный номер");
headers.append("uuid");
ui->tableDevicesWidget->setHorizontalHeaderLabels(headers);
ui->tableDevicesWidget->horizontalHeader()->setStretchLastSection(true);
MainWindow::ReadJson();
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(slotTimerAlarm()));
interval = 1000;
timer->start(interval);
}
MainWindow::~MainWindow()
{
QString command = "rm /tmp/" + FILE_NAME;
delete timer;
system(command.toLocal8Bit());
delete ui;
}
//Как выглядит json
//{
// "blockdevices": [
// {"name": "sda", "serial": null, "uuid": null,
// "children": [
// {"name": "sda1", "serial": null, "uuid": "f583e202-a8af-468f-8ba0-a247969119dc"},
// {"name": "sda2", "serial": null, "uuid": null},
// {"name": "sda5", "serial": null, "uuid": "38cd2fff-b608-4291-babc-8ac0c48cc9e1"},
// {"name": "sda6", "serial": null, "uuid": "9928712e-afcc-47ff-933f-b70a8ba2c0af"}
// ]
// },
// {"name": "sdb", "serial": "60A44C4139D4BFB0E99500C4", "uuid": null,
// "children": [
// {"name": "sdb1", "serial": null, "uuid": "3EC4-4836"}
// ]
// },
// {"name": "sr0", "serial": "10000000000000000001", "uuid": "2022-07-23-07-54-49-00"}
// ]
//}
QString createReadFile(){
QString command = "lsblk -o name,serial,uuid --json > /tmp/" + FILE_NAME;
system(command.toLocal8Bit());
QString val;
QFile file("/tmp/" + FILE_NAME);
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
return val;
}
void MainWindow::ReadJson(){
QString val = createReadFile();
QJsonDocument document = QJsonDocument::fromJson(val.toUtf8());
QJsonObject jObject = document.object();
QJsonArray jArr = jObject["blockdevices"].toArray();
int i = 0;
//очистка таблицы
ui->tableDevicesWidget->clearContents();
ui->tableDevicesWidget->setRowCount(0);
QVector<QTableWidgetItem*> a;
//заполнение таблицы
foreach (const QJsonValue & value, jArr) {
QJsonObject obj = value.toObject();
if ((obj["name"].toString().contains("sda")))
continue;
ui->tableDevicesWidget->insertRow(i);
QTableWidgetItem * name = new QTableWidgetItem(obj["name"].toString());
QTableWidgetItem * serial = new QTableWidgetItem(obj["serial"].toString());
QTableWidgetItem * uuid = new QTableWidgetItem(obj["uuid"].toString());
ui->tableDevicesWidget->setItem(i, 0, name);
ui->tableDevicesWidget->setItem(i, 1, serial);
ui->tableDevicesWidget->setItem(i, 2, uuid);
QJsonArray childrenArr = obj["children"].toArray();
//достаём ДЕТЕЙ из основного объекта
foreach (const QJsonValue & childValue, childrenArr) {
i++;
QJsonObject obj = childValue.toObject();
ui->tableDevicesWidget->insertRow(i);
QTableWidgetItem * name = new QTableWidgetItem(obj["name"].toString());
QTableWidgetItem * serial = new QTableWidgetItem(obj["serial"].toString());
QTableWidgetItem * uuid = new QTableWidgetItem(obj["uuid"].toString());
ui->tableDevicesWidget->setItem(i, 0, name);
ui->tableDevicesWidget->setItem(i, 1, serial);
ui->tableDevicesWidget->setItem(i, 2, uuid);
}
i++;
}
}
void MainWindow::slotTimerAlarm()
{
MainWindow::ReadJson();
}
void MainWindow::on_intervalPushButton_clicked()
{
QString s = ui->intervalLineEdit->text();
int newInterval = s.toInt();
if(newInterval >= 1000){
interval = newInterval;
timer->stop();
timer->start(interval);
}
}