当前位置: 首页 > news >正文

qt6 c++操作qtableview和yaml

  • 保存qtableview数据到yaml文件
  • 从yaml文件读取数据到qtableview

qtableview在UI界面拖放。
代码是问chat百度的深度探索。

- name: a1address: db1.dbw10type: int
- name: a2address: db1.dbx1.0type: bool

在这里插入图片描述

写到yaml,写前检查

bool plot1::isRowValid(const QStandardItemModel* model, int row) {std::array<int, 3> cols = { 0,1,2 };return std::all_of(cols.begin(), cols.end(), [&](int col) {QStandardItem* item = model->item(row, col);return item && !item->text().isEmpty();});
}

用的库是yaml-cpp 0.8,需要cmake编译。

void plot1::saveToYaml() {YAML::Emitter emitter;emitter << YAML::BeginSeq;for (int row = 0; row < g_model->rowCount(); ++row) {if (!isRowValid(g_model, row)) continue;emitter << YAML::BeginMap<< YAML::Key << "name" << YAML::Value << g_model->item(row, 0)->text().toStdString()<< YAML::Key << "address" << YAML::Value << g_model->item(row, 1)->text().toStdString()<< YAML::Key << "type" << YAML::Value << g_model->item(row, 2)->text().toStdString()<< YAML::EndMap;}emitter << YAML::EndSeq;std::ofstream fout("data.yaml");fout << emitter.c_str();
}

读取yaml

//检查项目行不为空 2
bool plot1::isItemValid(YAML::const_iterator it) {std::array<std::string, 3> cols = { "name","address","type" };return std::all_of(cols.begin(), cols.end(), [=](std::string col) {std::string s = (*it)[col].as<std::string>();return s.length() > 0;});
}//读取yaml到表
void plot1::loadYaml()
{try {YAML::Node config = YAML::LoadFile("data.yaml");g_model->removeRows(0, g_model->rowCount());for (YAML::const_iterator it = config.begin(); it != config.end(); ++it) {if (!isItemValid(it)) continue;QList<QStandardItem*> items;items << new QStandardItem(QString::fromStdString((*it)["name"].as<std::string>()));items << new QStandardItem(QString::fromStdString((*it)["address"].as<std::string>()));items << new QStandardItem(QString::fromStdString((*it)["type"].as<std::string>()));g_model->appendRow(items);}}catch (YAML::Exception& e) {qDebug() << "YAML Error:" << e.what();}
}
http://www.xdnf.cn/news/505405.html

相关文章:

  • 使用 CodeBuddy 开发一款富交互的屏幕录制与注释分享工具开发纪实
  • C语言查漏补缺
  • Codeforces Round 1024 (Div.2)
  • 【C/C++】C++返回值优化:RVO与NRVO全解析
  • 安全性(三):信息安全的五要素及其含义
  • Python-92:最大乘积区间问题
  • 从AI系统到伦理平台:技术治理的开放转向
  • docker部署第一个Go项目
  • 语音转文字并进行中英文翻译
  • 【JavaScript】 js 基础知识强化复习
  • 2025系统架构师---选择题知识点(押题)
  • JavaScript基础-作用域链
  • vue3: amap using typescript
  • 【2025 技术指南】如何创建和配置国际版 Apple ID
  • DeepSeek 赋能社会科学:解锁研究新范式
  • 第三十四节:特征检测与描述-SIFT/SURF 特征 (专利算法)
  • JavaScript基础-对象的相关概念
  • NestJS——日志、NestJS-logger、pino、winston、全局异常过滤器
  • ORACLE数据库实例报错ORA-00470: LGWR process terminated with error宕机问题分析报告
  • JavaScript 的编译与执行原理
  • IT运维的365天--026 视频下载相关
  • 常见平方数和立方数的计算
  • 简单网络交换、路由-华三RRPP以太环网
  • 电商项目-品牌管理微服务开发
  • OpenHarmony外设驱动使用 (二),Camera
  • 【大模型面试每日一题】Day 21:对比Chain-of-Thought(CoT)与Self-Consistency在复杂推理任务中的优劣
  • 线程同步学习
  • 8天Python从入门到精通【itheima】-11~13
  • SpringBootAdmin:全方位监控与管理SpringBoot应用
  • nt!MiInitializePfn函数分析之nt!MiPfPutPagesInTransition函数的关键一步