通过json对控件的某些状态做持久化
json格式
struct Window
{
bool magnetic = false;//磁力吸状态
bool toolBox = false; //工具箱状态
bool isMain = false;//是否是主窗口的组件化window
int zpos = 0; //z轴顺序
int toolPosx = 0;
int toolPosy = 0;
int toolOffsetx = InValidNumber;
int toolOffsety = InValidNumber;
int selectedIndex = 0;
std::string defaultPanel;//默认面板ID 为空时 选择第一个面板
std::string windowID;//唯一标识
std::string toolboxSelectTab;
std::unordered_map<std::string, Panel> panels;
Json::Value value;//原始字段 用于精确还原json布局
void Parse(const Json::Value & v);
Json::Value Obtain();
bool IsValidOffset(){ return toolOffsetx != InValidNumber && toolOffsety != InValidNumber; }
};
将从文件中的json格式内容解析到字段中:
void Window::Parse(const Json::Value & v)
{
value = v;
ObtainStringValue(value, "windowID", windowID);
ObtainStringValue(value, "toolboxSelectTab", toolboxSelectTab);
ObtainBoolValue(value, "isMain", isMain);
ObtainIntValue(value, "zpos", zpos);
ObtainStringValue(value, "selectedPanel", selectedPanel);
ObtainBoolValue(value, "toolbox", toolBox);
ObtainBoolValue(value, "magnetic", magnetic);
if (IsValueValid(value, "toolboxPos"))
{
const Json::Value & toolboxPos = value["toolboxPos"];
ObtainIntValue(toolboxPos, "toolPosx", toolPosx);
ObtainIntValue(toolboxPos, "toolPosy", toolPosy);
}
if (IsValueValid(value, "toolboxOffset"))
{
const Json::Value & toolboxOffset = value["toolboxOffset"];
ObtainIntValue(toolboxOffset, "toolOffsetx", toolOffsetx);
ObtainIntValue(toolboxOffset, "toolOffsety", toolOffsety);
}
if (IsArrayValid(value, "panels"))
{
Json::Value json_panels = value["panels"];
for (int i = 0; i < json_panels.size(); ++i)
{
Panel panel;
panel.Parse(json_panels[i]);
panels.insert({ panel.panelID, panel });
}
}
}
将字段存储到json格式的变量中:
Json::Value Window::Obtain()
{
ForceInsertStringValue(value, "windowID", windowID);
ForceInsertStringValue(value, "toolboxSelectTab", toolboxSelectTab);
InsertBoolValue(value, "isMain", isMain);
ForceInsertIntValue(value, "zpos", zpos);
ForceInsertStringValue(value, "selectedPanel", selectedPanel);
InsertBoolValue(value, "toolbox", toolBox);
InsertBoolValue(value, "magnetic", magnetic);
ForceInsertIntValue(value["toolboxPos"], "toolPosx", toolPosx);
ForceInsertIntValue(value["toolboxPos"], "toolPosy", toolPosy);
ForceInsertIntValue(value["toolboxOffset"], "toolOffsetx", toolOffsetx);
ForceInsertIntValue(value["toolboxOffset"], "toolOffsety", toolOffsety);
value["panels"].clear();
for (auto iter = panels.begin(); iter != panels.end(); ++iter)
{
value["panels"].append(iter->second.Obtain());
}
return value;
}
都存储到value中,在将value转为string即可
std::string Serialize::Deserialization()
{
return serialize.Obtain().toStyledString();
}
在存储期间遇到了一种情况就是listWidget所对应的组id映射关系非常复杂,需要多加很多字段进行存储和map进行映射,所以在TListWidget(公司重写的控件)等控件中增加一个QVarient变量和set及get方法用来存储绑定的信息
#define DECLEARE_TIGER_DATA \
private: \
QVariant m_pTigerData; \
public: \
void SetTigerData(const QVariant & var){ m_pTigerData = var; } \
QVariant GetTigerData() const{ return m_pTigerData; }
增加了一个宏
调用方法:
QVariant variant;
variant.setValue(group.groupID);
normalWidget->SetTigerData(variant);
if (m_pTabWidget != nullptr)
{
TListWidget* currentWidget = qobject_cast<TListWidget *>(m_pTabWidget->currentWidget());
if (currentWidget != nullptr)
{
window.toolboxSelectTab = currentWidget->GetTigerData().value<std::string>();
}
}
未注册类型的注册:
#include <QMetaType>
Q_DECLARE_METATYPE(std::string);