一、相机的数据的持久化
1. 摄像机数据类型
struct XCameraData
{
char name[1024] = { 0 };
char url[1024] = { 0 };
char sub_url[1024] = { 0 };
char save_path[1024] = { 0 };
}
2. 相机数据的增删改查
class XCameraConfig
{
public:
//增删改查
bool Push(XCameraData &data);
bool Del(int &index);
bool Set(int &index, XCameraData &data);
XCameraData& Get(int &index);
//持久化
bool Save(const char *url);
bool Load(const char *url);
private:
std::vector<XCameraData> cam_datas_;
std::<mutex> mtx;
}
bool XCameraConfig::Push(XCameraData &data)
{
unique_lock<mutex> lock(mtx);
cam_datas_.push_back(data);
return true;
}
bool XCameraConfig::Del(int &index)
{
if(index < 0 || index > cam_datas.size())
{
return false;
}
unique_lock<mutex> lock(mtx);
cam_datas_.erase(cam_datas.begin() + index);
return true;
}
bool XCameraConfig::Set(int &index, XCameraData &data)
{
if(index < 0 || index > cam_datas.size())
{
return false;
}
unique_lock<mutex> lock(mtx);
cam_datas_[index] = data;
return true;
}
XCameraData& XCameraConfig::Get(int &index)
{
if(index < 0 || index > cam_datas.size())
{
return false;
}
unique_lock<mutex> lock(mtx);
return cam_datas[index];
}
3. 相机数据的持久化
bool XCameraConfig::Save(const char *url)
{
ofstream ofs(url, ios::binary);
unique_lock<mutex> lock(mtx_);
for(auto data : cam_datas)
{
ofs.write((char *)&data, sizeof(data));
}
return true;
}
bool XCameraConfig::Load(const char *url)
{
ifstream ifs(url, ios::binary);
unique_lock<mutex> lock(mtx_);
cam_datas.clear();
XCameraData data;
while(true)
{
ifs.read((char *)data, sizeof(data));
if(ifs.gcount() != sizeof(data))
{
ifs.close();
return true;
}
cam_datas.push_back(data);
}
}
二、左侧相机列表
1. 显示相机列表
- 左侧添加List Widget 控件
- 加载文件中存在的相机
- 将存在的相机显示在左侧
void RefreshCams()
{
ui.list->clear(); //清楚原有的显示
auto xc = XCameraConfig::GetInstance();
xc.Load(path); //加载现在的相机
for(int i = 0; i < xc.getcount(); i++)
{
auto xd = xc.Get(i); //获取每个相机数据
auto item = new QListWidgetItem(QIcon(":/XViewer10/img/cam.png"), C(xd->cam_name)); //为每个相机创建图标和名字
ui.list->addItem(item); //添加到左侧列表
}
}
2. 左侧相机的增删改查
-
创建一个新增的按钮
-
新增按钮的槽函数是一个对话框
connect(ui.add, SIGNAL(clicked()), this, SLOG(AddCams()));
创建一个对话框
QDialog dialog;
dialog.resize(600, 400);
QFormLayout layout; 布局器
dialog.setLayout(&layout);
QLineEdit nameline; 名字栏
layout.addRow(C("名字"), nameline);
QLineEdit urlline; 主码流
layout.addRow(C("主码流"), nameline);
QLineEdit suburlline; 辅码流
layout.addRow(C("辅码流"), nameline);
QLineEdit saveline; 存储路径
layout.addRow(C("存储路径"), nameline);
保存相机的数据
QPushButton btn(C("保存"));
layout.addRow("", btn);
connect(&btn, SIGNAL(clicked()), &dialog, SLOT(accept()));
保存到config
for (;;)
{
if (dlg.exec() == QDialog::Accepted) //点击了保存
{
if (name_edit.text().isEmpty())
{
QMessageBox::information(0, "error", C("请输入名称"));
continue;
}
if (url_edit.text().isEmpty())
{
QMessageBox::information(0, "error", C("请输入主码流"));
continue;
}
if (sub_url_edit.text().isEmpty())
{
QMessageBox::information(0, "error", C("请输入辅码流"));
continue;
}
if (save_path_edit.text().isEmpty())
{
QMessageBox::information(0, "error", C("请输入保存目录"));
continue;
}
break;
}
return;
}
XCameraData data;
strcpy(data.name, name_edit.text().toLocal8Bit());
strcpy(data.url, url_edit.text().toLocal8Bit());
strcpy(data.sub_url, sub_url_edit.text().toLocal8Bit());
strcpy(data.save_path, save_path_edit.text().toLocal8Bit());
XCameraConfig::Instance()->Push(data); //插入数据
XCameraConfig::Instance()->Save(CAM_CONF_PATH); //保存到文件
RefreshCams(); //刷新显示
刷新列表