Qt 配置文件QSettings读取以及中文问题
2016-06-21 11:57
507 查看
QSettings settings("xxxx.ini",QSettings::IniFormat); settings.setIniCodec(QTextCodec::codecForName("GB2312")); //在此添加设置,即可读写ini文件中的中文setup.ini文件内容: [Rooms] 1\RoomName=Room1 1\PC\1\PCName=PC1 1\PC\1\IP=192.168.1.121 1\PC\1\App=C:\\chess.exe 1\PC\1\MACAddress=A0-2C-36-9E-94-FB 1\PC\2\PCName=PC2 1\PC\2\IP=192.168.1.152 1\PC\2\App=D:\\chess.exe 1\PC\2\MACAddress=38-D5-47-00-1E-81 1\PC\3\PCName=PC3 1\PC\3\IP=192.168.1.153 1\PC\3\App=D:\\chess.exe 1\PC\3\MACAddress=00-00-00-00-00-00 1\PC\size=3 2\RoomName=Room2 2\PC\1\PCName=PC1 2\PC\1\IP=192.168.1.121 2\PC\1\App=C:\\chess.exe 2\PC\1\MACAddress=00-00-00-00-00-00 2\PC\2\PCName=PC2 2\PC\2\IP=192.168.1.152 2\PC\2\App=D:\\chess.exe 2\PC\2\MACAddress=38-D5-47-00-1E-81 2\PC\3\PCName=PC3 2\PC\3\IP=192.168.1.153 2\PC\3\App=D:\\chess.exe 2\PC\3\MACAddress=00-00-00-00-00-00 2\PC\4\PCName=PC4 2\PC\4\IP=192.168.1.154 2\PC\4\App=D:\\chess.exe 2\PC\3\MACAddress=00-00-00-00-00-00 2\PC\size=4 3\RoomName=Room5 3\PC\1\PCName=PC1 3\PC\1\IP=192.168.1.121 3\PC\1\App=C:\\chess.exe|D:\\A.exe|D:\\B.exe|D:\\C.exe 3\PC\1\MACAddress=00-00-00-00-00-00 3\PC\2\PCName=PC2 3\PC\2\IP=192.168.1.152 3\PC\2\App=D:\\chess.exe|D:\\A.exe|D:\\B.exe|D:\\C.exe 3\PC\2\MACAddress=38-D5-47-00-1E-81 3\PC\3\PCName=PC3 3\PC\3\IP=192.168.1.153 3\PC\3\App=D:\\chess.exe|D:\\A.exe|D:\\B.exe|D:\\C.exe 3\PC\3\MACAddress=00-00-00-00-00-00 3\PC\4\PCName=PC4 3\PC\4\IP=192.168.1.154 3\PC\4\App=D:\\chess.exe|D:\\A.exe|D:\\B.exe|D:\\C.exe 3\PC\4\MACAddress=00-00-00-00-00-00 3\PC\size=4 size=3 // 读取配置文件代码 QSettings* configIniRead = new QSettings( QCoreApplication::applicationDirPath()+"/setup.ini", QSettings::IniFormat); int roomCnt = configIniRead->beginReadArray("rooms"); for (int i = 0; i < roomCnt; ++i) { configIniRead->setArrayIndex(i); QString roomName = configIniRead->value("RoomName").toString(); int pcCnt = configIniRead->beginReadArray("PC"); for (int j = 0; j < pcCnt; ++j) { configIniRead->setArrayIndex(j); QString name = configIniRead->value("PCName").toString(); QString ip = configIniRead->value("IP").toString(); QString app = configIniRead->value("App").toString(); QString mac = configIniRead->value("MacAddress").toString(); //qDebug()<<app; QStringList list; list<<name<<ip<<app<<mac; rooms[roomName].push_back(list); } configIniRead->endArray(); } configIniRead->endArray();
setup.ini内容: [General] isServer=true 读取代码: QString appDirPath = QCoreApplication::applicationDirPath(); QSettings* configIniRead = new QSettings(appDirPath+"/setup.ini", QSettings::IniFormat); bool isServer = false; if( configIniRead->allKeys().size() > 0 ) { isServer = configIniRead->value("isServer").toBool(); //httpServerPort = configIniRead->value("httpServerPort").toInt(); qDebug() <<isServer<< "use setup.ini setting." ; } else { qDebug() << "have no setup.ini , use default setting ." ; }
QString appDirPath = QCoreApplication::applicationDirPath(); QSettings* configIniRead = new QSettings(appDirPath+"/setup.ini", QSettings::IniFormat); // test /* configIniRead->beginGroup("mainwindow"); configIniRead->setValue("size", 122); configIniRead->setValue("fullScreen", 1); configIniRead->endGroup(); configIniRead->beginGroup("outputpanel"); configIniRead->setValue("visible", 1); configIniRead->endGroup(); settings.setValue("fridge/color", QColor(Qt::white)); settings.setValue("fridge/size", QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); QStringList keys = settings.allKeys(); // keys: ["fridge/color", "fridge/size", "sofa", "tv"] QStringList keys = settings.childKeys(); // keys: ["sofa", "tv"] QStringList groups = settings.childGroups(); // groups: ["fridge"] configIniRead->beginWriteArray("logins"); for (int i = 0; i < 3; ++i) { configIniRead->setArrayIndex(i); configIniRead->setValue("userName", i); configIniRead->setValue("password", "0a"); } configIniRead->endArray(); */
相关文章推荐
- [Q学习]13 Qt状态机框架——简单的状态机
- Qt Drag and Drop
- qt项目的打包发布
- QTP中DataTable用例取值与循环
- QTP 利用DOM实现拖动浏览器滚动条
- Qt项目管理(33个规则)
- Qt5 json文件"读"与"写"
- QT笔记之QLineEdit自动补全以及控件提升
- 关于Qt线程中的事件循环
- Qtcreator 程序添加版本信息
- Qt国际化多国语言和发布例子
- [Q学习]12 Qt事件系统——发送事件
- Windows Server环境下MQTT服务器的搭建 07/17
- 树莓派3上安装Qt5
- 关于qt中的tr()函数
- QT5中文显示乱码解决方法
- QAction类
- win10操作系统 Qt Creator 无法调试
- MFC和QT的区别
- QT开发