Qt的QTreeWidget来显示嵌套结构体的成员,并以树形结构展示。
#include <QApplication>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QString>
#include <cstdint>
// 假设这些是你的结构体定义
struct BaseMeterParam_Type {
int param1;
float param2;
};
struct NetParam_Type {
QString ipAddress;
int port;
};
struct TimingUploadParam_Type {
int interval;
bool enabled;
};
struct LadderParam_Type {
int stepCount;
float stepValue;
};
struct DeviceParam_Type {
BaseMeterParam_Type mt_BaseMeter;
NetParam_Type mt_Net;
TimingUploadParam_Type mt_TimingUpload;
LadderParam_Type mt_RunLadder;
uint16_t m_None;
uint16_t m_Crc16;
};
// 模板函数,用于将结构体成员添加到树中
template<typename T>
void addMemberToTree(QTreeWidgetItem* parent, const QString& name, const T& value) {
QTreeWidgetItem* item = new QTreeWidgetItem(parent);
item->setText(0, name);
item->setText(1, QString::number(value));
}
// 特化模板函数,用于处理QString
template<>
void addMemberToTree<QString>(QTreeWidgetItem* parent, const QString& name, const QString& value) {
QTreeWidgetItem* item = new QTreeWidgetItem(parent);
item->setText(0, name);
item->setText(1, value);
}
// 特化模板函数,用于处理bool
template<>
void addMemberToTree<bool>(QTreeWidgetItem* parent, const QString& name, const bool& value) {
QTreeWidgetItem* item = new QTreeWidgetItem(parent);
item->setText(0, name);
item->setText(1, value ? "true" : "false");
}
// 宏定义,用于遍历结构体成员
#define ADD_MEMBER_TO_TREE(parent, member) addMemberToTree(parent, #member, deviceParam.member)
// 定义一个函数来遍历DeviceParam_Type的成员并添加到树中
void traverseDeviceParamToTree(QTreeWidget* tree, const DeviceParam_Type& deviceParam) {
QTreeWidgetItem* root = tree->invisibleRootItem();
// 添加mt_BaseMeter成员
QTreeWidgetItem* mt_BaseMeterItem = new QTreeWidgetItem(root);
mt_BaseMeterItem->setText(0, "mt_BaseMeter");
ADD_MEMBER_TO_TREE(mt_BaseMeterItem, mt_BaseMeter.param1);
ADD_MEMBER_TO_TREE(mt_BaseMeterItem, mt_BaseMeter.param2);
// 添加mt_Net成员
QTreeWidgetItem* mt_NetItem = new QTreeWidgetItem(root);
mt_NetItem->setText(0, "mt_Net");
ADD_MEMBER_TO_TREE(mt_NetItem, mt_Net.ipAddress);
ADD_MEMBER_TO_TREE(mt_NetItem, mt_Net.port);
// 添加mt_TimingUpload成员
QTreeWidgetItem* mt_TimingUploadItem = new QTreeWidgetItem(root);
mt_TimingUploadItem->setText(0, "mt_TimingUpload");
ADD_MEMBER_TO_TREE(mt_TimingUploadItem, mt_TimingUpload.interval);
ADD_MEMBER_TO_TREE(mt_TimingUploadItem, mt_TimingUpload.enabled);
// 添加mt_RunLadder成员
QTreeWidgetItem* mt_RunLadderItem = new QTreeWidgetItem(root);
mt_RunLadderItem->setText(0, "mt_RunLadder");
ADD_MEMBER_TO_TREE(mt_RunLadderItem, mt_RunLadder.stepCount);
ADD_MEMBER_TO_TREE(mt_RunLadderItem, mt_RunLadder.stepValue);
// 添加其他成员
ADD_MEMBER_TO_TREE(root, m_None);
ADD_MEMBER_TO_TREE(root, m_Crc16);
}
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QTreeWidget>
class DeviceParamWidget : public QWidget {
public:
DeviceParamWidget(const DeviceParam_Type& deviceParam) {
// 创建树
QTreeWidget* tree = new QTreeWidget(this);
tree->setColumnCount(2);
tree->setHeaderLabels({"Name", "Value"});
// 遍历设备参数并添加到树中
traverseDeviceParamToTree(tree, deviceParam);
// 设置布局
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(tree);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
DeviceParam_Type deviceParam = {
{1, 2.0f},
{"192.168.1.1", 8080},
{60, true},
{10, 0.5f},
0,
0xFFFF
};
DeviceParamWidget widget(deviceParam);
widget.resize(400, 300);
widget.show();
return app.exec();
}