新闻详情

新闻详情

首页 / 资讯中心 / 详情

使用QSetting读取ini中的信息并打印

发布时间:2026/11/15 10:13:37来源:尧图网络
使用QSetting读取ini中的信息并打印
QT - gui CONFIG c11 console CONFIG - app_bundle SOURCES main.cpp#include QCoreApplication #include QSettings #include QDebug int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 使用源码目录的绝对路径 QString configPath d:/QtCode/IniTest/config.ini; qDebug() configPath: configPath; QSettings settings(configPath, QSettings::IniFormat); // 调试检查文件是否找到 if (settings.allKeys().isEmpty()) { qDebug() 警告: 未读取到任何配置项请检查文件路径; } qDebug() 方式一: allKeys 遍历所有键值 ; const QStringList allKeys settings.allKeys(); for (const QString key : allKeys) { qDebug().noquote() key settings.value(key).toString(); } qDebug() \n 方式二: 按 section/key 读取 ; qDebug() user/name settings.value(user/name).toString(); qDebug() user/age settings.value(user/age).toInt(); qDebug() server/host settings.value(server/host).toString(); qDebug() server/port settings.value(server/port).toInt(); qDebug() server/notExist (默认值) settings.value(server/notExist, 12345).toInt(); qDebug() logging/enabled settings.value(logging/enabled).toBool(); qDebug() \n 方式三: beginGroup 分组读取 ; settings.beginGroup(server); qDebug() host settings.value(host).toString(); qDebug() port settings.value(port).toInt(); qDebug() timeout settings.value(timeout).toInt(); settings.endGroup(); qDebug() \n 方式四: 遍历所有分组 ; const QStringList groups settings.childGroups(); for (const QString group : groups) { qDebug() section: group; } return 0; }config.ini[user] name张三 age25 emailzhangsanexample.com [server] host192.168.1.100 port8080 timeout3000 [logging] enabledtrue leveldebug读到了但是中文部分是乱码。修改程序QSettings settings(configPath, QSettings::IniFormat); settings.setIniCodec(UTF-8);可以了继续修改使用相对路径QString configPath QCoreApplication::applicationDirPath() /config.ini; qDebug() configPath: configPath;构造函数QSettings(config.ini, QSettings::IniFormat)指定文件路径和格式。不指定格式时默认用注册表Windows或 plistmacOS读 INI 必须显式指定QSettings::IniFormat。键名规则INI 中的[section]变成section/key形式的扁平键如[user]下的name用user/name访问。返回 QVariantvalue()返回QVariant需要.toString()、.toInt()、.toBool()等转换。QSettings 会自动把true/false识别为布尔、纯数字识别为数值。提问如果组名为General,这时候我就读不到信息了为什么[General] localAddress192.168.68.1 peerAddress192.168.68.100 peerPort1234 localPort1235qDebug() localAddress settings.value(General/localAddress).toString();打印:localAddress 这是 QSettings 的一个特殊行为[General]是 QSettings 的保留组名。原因QSettings 把 INI 文件中的[General]组视为顶层无组前缀。也就是说[General] name张三 age25在 QSettings 看来这两个键没有组前缀读取方式是settings.value(name); // 正确 settings.value(General/name); // 错误读不到测试:const QStringList allKeys settings.allKeys(); for (const QString key : allKeys) { qDebug().noquote() key settings.value(key).toString(); }组为General时:localAddress 192.168.68.1localPort 1235peerAddress 192.168.68.100peerPort 1234组为info时:info/localAddress 192.168.68.1info/localPort 1235info/peerAddress 192.168.68.100info/peerPort 1234总结[General]是 QSettings 的约定等同于不属于任何组的顶层键如果你需要统一用组名/键名的方式访问避免使用General作为组名改用其他名称如[common]、[default]、[config]等即可
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/15 4:54:16

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/15 2:04:12

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/14 10:11:15

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/14 17:14:38

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/14 16:35:50

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/15 4:06:36

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉