Qt creator 判断结构体原始数据和修改后数据是否有变动(是否修改或者成员变动),展示完整示例代码。结构体如下:struct T_cjChannelList{ QString name; QString desc; int comType; int interFrameDelay; QString portName; int baudRate; int dataBits; int stopBits; QString parity; QString ip; QString port;}struct T_devList_datapoints{ QString dataId; QString dataDesc; int funcode; int registerAddr; int dataType; int appType; int bit; QString byteOder; QString crcOder; float coef; int calB; }struct T_Devs{ QString devName; QString devDesc; int devAddr; int protocolType; int delayTime; QList<T_devList_datapoints> dataPoints;}struct T_devList{ String channel; QLsit<T_Dev> devs;}struct T_zf_dataPoints{ QString pointId; QString channelId; QString devID; QString dataId; float coef; int calB;}struct T_zfChannelList{ QString name; QString desc; int comType; QString topic; int delayTime; QList<T_zf_dataPoints> dataPoints;}struct Cfg_t{ QList<T_cjChannelList> cjChannelList; QList<T_devList> devList; QList<T_zfChannelList> zfChannelList;}
时间: 2024-03-05 09:54:51 浏览: 76
Qt creator 调试器
可以通过重载结构体的比较运算符来判断结构体原始数据和修改后数据是否有变动。以下是一个示例代码:
```c++
#include <QDebug>
#include <QList>
struct T_cjChannelList {
QString name;
QString desc;
int comType;
int interFrameDelay;
QString portName;
int baudRate;
int dataBits;
int stopBits;
QString parity;
QString ip;
QString port;
bool operator==(const T_cjChannelList& other) const {
return (name == other.name && desc == other.desc && comType == other.comType &&
interFrameDelay == other.interFrameDelay && portName == other.portName &&
baudRate == other.baudRate && dataBits == other.dataBits &&
stopBits == other.stopBits && parity == other.parity && ip == other.ip &&
port == other.port);
}
};
struct T_devList_datapoints {
QString dataId;
QString dataDesc;
int funcode;
int registerAddr;
int dataType;
int appType;
int bit;
QString byteOder;
QString crcOder;
float coef;
int calB;
bool operator==(const T_devList_datapoints& other) const {
return (dataId == other.dataId && dataDesc == other.dataDesc && funcode == other.funcode &&
registerAddr == other.registerAddr && dataType == other.dataType &&
appType == other.appType && bit == other.bit && byteOder == other.byteOder &&
crcOder == other.crcOder && coef == other.coef && calB == other.calB);
}
};
struct T_Devs {
QString devName;
QString devDesc;
int devAddr;
int protocolType;
int delayTime;
QList<T_devList_datapoints> dataPoints;
bool operator==(const T_Devs& other) const {
if (devName != other.devName || devDesc != other.devDesc || devAddr != other.devAddr ||
protocolType != other.protocolType || delayTime != other.delayTime ||
dataPoints.size() != other.dataPoints.size()) {
return false;
}
for (int i = 0; i < dataPoints.size(); i++) {
if (dataPoints[i] != other.dataPoints[i]) {
return false;
}
}
return true;
}
};
struct T_devList {
QString channel;
QList<T_Devs> devs;
bool operator==(const T_devList& other) const {
if (channel != other.channel || devs.size() != other.devs.size()) {
return false;
}
for (int i = 0; i < devs.size(); i++) {
if (devs[i] != other.devs[i]) {
return false;
}
}
return true;
}
};
struct T_zf_dataPoints {
QString pointId;
QString channelId;
QString devID;
QString dataId;
float coef;
int calB;
bool operator==(const T_zf_dataPoints& other) const {
return (pointId == other.pointId && channelId == other.channelId && devID == other.devID &&
dataId == other.dataId && coef == other.coef && calB == other.calB);
}
};
struct T_zfChannelList {
QString name;
QString desc;
int comType;
QString topic;
int delayTime;
QList<T_zf_dataPoints> dataPoints;
bool operator==(const T_zfChannelList& other) const {
if (name != other.name || desc != other.desc || comType != other.comType ||
topic != other.topic || delayTime != other.delayTime ||
dataPoints.size() != other.dataPoints.size()) {
return false;
}
for (int i = 0; i < dataPoints.size(); i++) {
if (dataPoints[i] != other.dataPoints[i]) {
return false;
}
}
return true;
}
};
struct Cfg_t {
QList<T_cjChannelList> cjChannelList;
QList<T_devList> devList;
QList<T_zfChannelList> zfChannelList;
bool operator==(const Cfg_t& other) const {
if (cjChannelList.size() != other.cjChannelList.size() ||
devList.size() != other.devList.size() ||
zfChannelList.size() != other.zfChannelList.size()) {
return false;
}
for (int i = 0; i < cjChannelList.size(); i++) {
if (cjChannelList[i] != other.cjChannelList[i]) {
return false;
}
}
for (int i = 0; i < devList.size(); i++) {
if (devList[i] != other.devList[i]) {
return false;
}
}
for (int i = 0; i < zfChannelList.size(); i++) {
if (zfChannelList[i] != other.zfChannelList[i]) {
return false;
}
}
return true;
}
};
int main() {
Cfg_t original = {
{ {"name1", "desc1", 1, 100, "port1", 9600, 8, 1, "N", "192.168.1.1", "1001"},
{"name2", "desc2", 2, 200, "port2", 115200, 7, 2, "O", "192.168.1.2", "1002"} },
{ {"channel1", { {"devName1", "devDesc1", 1, 1, 1, { {"dataId1", "dataDesc1", 1, 1, 1, 1, 1, "MSB", "CRC16", 1.0, 1} } } } },
{"channel2", { {"devName2", "devDesc2", 2, 2, 2, { {"dataId2", "dataDesc2", 2, 2, 2, 2, 2, "LSB", "CRC32", 2.0, 2} } } } } },
{ {"name3", "desc3", 3, "topic1", 300, { {"pointId1", "channelId1", "devID1", "dataId1", 3.0, 3} } },
{"name4", "desc4", 4, "topic2", 400, { {"pointId2", "channelId2", "devID2", "dataId2", 4.0, 4} } } }
};
Cfg_t modified = {
{ {"name1", "desc1", 1, 100, "port1", 9600, 8, 1, "N", "192.168.1.1", "1001"},
{"name2", "desc2", 2, 200, "port2", 115200, 7, 2, "O", "192.168.1.2", "1002"} },
{ {"channel1", { {"devName1", "devDesc1", 1, 1, 1, { {"dataId1", "dataDesc1", 1, 1, 1, 1, 1, "MSB", "CRC16", 1.0, 1} } } } },
{"channel2", { {"devName2", "devDesc2", 2, 2, 2, { {"dataId2", "dataDesc2", 2, 2, 2, 2, 2, "LSB", "CRC32", 2.0, 2} } } } } },
{ {"name3", "desc3", 3, "topic1", 300, { {"pointId1", "channelId1", "devID1", "dataId1", 3.0, 3} } },
{"name4", "desc4", 4, "topic2", 400, { {"pointId2", "channelId2", "devID2", "dataId2", 4.0, 4} } } }
};
qDebug() << "Original and modified are equal: " << (original == modified);
modified.cjChannelList[0].name = "name5";
qDebug() << "Original and modified are equal: " << (original == modified);
modified.devList[0].channel = "channel3";
qDebug() << "Original and modified are equal: " << (original == modified);
modified.zfChannelList[0].dataPoints[0].pointId = "pointId3";
qDebug() << "Original and modified are equal: " << (original == modified);
return 0;
}
```
在上面的示例代码中,我们定义了多个嵌套结构体,包括 `T_cjChannelList`、`T_devList_datapoints`、`T_Devs`、`T_devList`、`T_zf_dataPoints`、`T_zfChannelList` 和 `Cfg_t`。在每个结构体中,我们都重载了比较运算符 `operator==`。在 `operator==` 中,我们逐个比较结构体中的成员变量是否相等。对于嵌套的结构体,我们可以递归比较其中的元素是否相同。如果结构体原始数据和修改后数据不同,我们返回 `false`。否则,我们返回 `true`。
在 `main` 函数中,我们首先定义了一个名为 `original` 的 `Cfg_t` 结构体并初始化它。然后,我们定义了一个名为 `modified` 的 `Cfg_t` 结构体,并将其中一个成员变量的值修改为不同的值。我们使用 `qDebug` 打印出 `original` 和 `modified` 是否相等的结果。由于 `modified` 结构体与 `original` 结构体不相等,因此第一次打印的结果为 `false`。然后,我们将 `modified` 结构体的值改回与 `original` 结构体相同,并再次打印两者是否相等的结果,此时结果为 `true`。最后,我们将 `modified` 结构体中的一个成员变量的值修改为不同的值,并再次打印两者是否相等的结果,此时结果为 `false`。
阅读全文