研究了以下Qt下ini文件的读写,不废话,上干货。
写入ini文件
WriteIni.cpp
1 void WriteIni::writeSettings() 2 { 3 QSettings settings("config.ini", QSettings::IniFormat); // 当前目录的INI文件 4 5 settings.beginGroup("mountinfo"); 6 settings.setValue("mount",6); 7 settings.endGroup(); 8 9 settings.beginGroup("v"); 10 settings.setValue("v1",14.2); 11 settings.setValue("v2",14.3); 12 settings.setValue("v3",14.4); 13 settings.setValue("v4",14.5); 14 settings.setValue("v5",14.6); 15 settings.setValue("v6",14.8); 16 settings.endGroup(); 17 18 settings.beginGroup("a"); 19 settings.setValue("a1",2.0); 20 settings.setValue("a2",2.1); 21 settings.setValue("a3",2.2); 22 settings.setValue("a4",2.3); 23 settings.setValue("a5",2.4); 24 settings.setValue("a6",2.5); 25 settings.endGroup(); 26 27 settings.beginGroup("p"); 28 settings.setValue("p1",126); 29 settings.setValue("p2",138); 30 settings.setValue("p3",139); 31 settings.setValue("p4",140); 32 settings.setValue("p5",141); 33 settings.setValue("p6",142); 34 settings.endGroup(); 35 36 settings.beginGroup("j"); 37 settings.setValue("j1",4.8); 38 settings.setValue("j2",4.9); 39 settings.setValue("j3",4.7); 40 settings.setValue("j4",4.6); 41 settings.setValue("j5",4.5); 42 settings.setValue("j6",4.4); 43 settings.endGroup(); 44 }
读取ini文件
ReadConfig.cpp
1 void GetConfigIni::getConfigMount() 2 { 3 QVariant strConfigValue; 4 QString strConfigGroup = ""; 5 QString strConfigKey = ""; 6 7 QString strfile; 8 9 //strfile = QDir::currentPath();//QCoreApplication::applicationDirPath(); 10 11 12 QString fileName = "config.ini"; 13 14 strfile += fileName; 15 QSettings user_cfg(strfile,QSettings::IniFormat); 16 user_cfg.setIniCodec("UTF-8"); 17 strConfigGroup = tr("mountinfo"); 18 user_cfg.beginGroup(strConfigGroup); 19 20 strConfigKey = "mount"; 21 strConfigValue = user_cfg.value(strConfigKey); 22 23 int Mount = strConfigValue.toInt(); 24 25 qDebug()<<"mount*************"<<Mount<<endl; 26 27 user_cfg.endGroup(); 28 29 strConfigGroup = tr("v"); 30 user_cfg.beginGroup(strConfigGroup); 31 32 strConfigKey = "v1"; 33 strConfigValue = user_cfg.value(strConfigKey); 34 qDebug() << "v1=*********"<<strConfigValue; 35 36 strConfigKey = "v2"; 37 strConfigValue = user_cfg.value(strConfigKey); 38 qDebug() << "v2=*********"<<strConfigValue; 39 40 strConfigKey = "v3"; 41 strConfigValue = user_cfg.value(strConfigKey); 42 qDebug() << "v3=*********"<<strConfigValue; 43 44 strConfigKey = "v4"; 45 strConfigValue = user_cfg.value(strConfigKey); 46 qDebug() << "v4=*********"<<strConfigValue; 47 48 strConfigKey = "v5"; 49 strConfigValue = user_cfg.value(strConfigKey); 50 qDebug() << "v5=*********"<<strConfigValue; 51 52 strConfigKey = "v6"; 53 strConfigValue = user_cfg.value(strConfigKey); 54 qDebug() << "v6=*********"<<strConfigValue; 55 56 user_cfg.endGroup(); 57 58 strConfigGroup = tr("a"); 59 user_cfg.beginGroup(strConfigGroup); 60 61 strConfigKey = "a1"; 62 strConfigValue = user_cfg.value(strConfigKey); 63 qDebug() << "a1=*********"<<strConfigValue; 64 65 strConfigKey = "a2"; 66 strConfigValue = user_cfg.value(strConfigKey); 67 qDebug() << "a2=*********"<<strConfigValue; 68 69 strConfigKey = "a3"; 70 strConfigValue = user_cfg.value(strConfigKey); 71 qDebug() << "a3=*********"<<strConfigValue; 72 73 strConfigKey = "a4"; 74 strConfigValue = user_cfg.value(strConfigKey); 75 qDebug() << "a4=*********"<<strConfigValue; 76 77 strConfigKey = "a5"; 78 strConfigValue = user_cfg.value(strConfigKey); 79 qDebug() << "a5=*********"<<strConfigValue; 80 81 strConfigKey = "a6"; 82 strConfigValue = user_cfg.value(strConfigKey); 83 qDebug() << "a6=*********"<<strConfigValue; 84 85 user_cfg.endGroup(); 86 87 strConfigGroup = tr("p"); 88 user_cfg.beginGroup(strConfigGroup); 89 90 strConfigKey = "p1"; 91 strConfigValue = user_cfg.value(strConfigKey); 92 qDebug() << "p1=*********"<<strConfigValue; 93 94 strConfigKey = "p2"; 95 strConfigValue = user_cfg.value(strConfigKey); 96 qDebug() << "p2=*********"<<strConfigValue; 97 98 strConfigKey = "p3"; 99 strConfigValue = user_cfg.value(strConfigKey); 100 qDebug() << "p3=*********"<<strConfigValue; 101 102 strConfigKey = "p4"; 103 strConfigValue = user_cfg.value(strConfigKey); 104 qDebug() << "p4=*********"<<strConfigValue; 105 106 strConfigKey = "p5"; 107 strConfigValue = user_cfg.value(strConfigKey); 108 qDebug() << "p5=*********"<<strConfigValue; 109 110 strConfigKey = "p6"; 111 strConfigValue = user_cfg.value(strConfigKey); 112 qDebug() << "p6=*********"<<strConfigValue; 113 114 user_cfg.endGroup(); 115 116 strConfigGroup = tr("j"); 117 user_cfg.beginGroup(strConfigGroup); 118 119 strConfigKey = "j1"; 120 strConfigValue = user_cfg.value(strConfigKey); 121 qDebug() << "j1=*********"<<strConfigValue; 122 123 strConfigKey = "j2"; 124 strConfigValue = user_cfg.value(strConfigKey); 125 qDebug() << "j2=*********"<<strConfigValue; 126 127 strConfigKey = "j3"; 128 strConfigValue = user_cfg.value(strConfigKey); 129 qDebug() << "j3=*********"<<strConfigValue; 130 131 strConfigKey = "j4"; 132 strConfigValue = user_cfg.value(strConfigKey); 133 qDebug() << "j4=*********"<<strConfigValue; 134 135 strConfigKey = "j5"; 136 strConfigValue = user_cfg.value(strConfigKey); 137 qDebug() << "j5=*********"<<strConfigValue; 138 139 strConfigKey = "j6"; 140 strConfigValue = user_cfg.value(strConfigKey); 141 qDebug() << "j6=*********"<<strConfigValue; 142 143 user_cfg.endGroup(); 144 145 146 147 }
ini文件
config.ini
1 [mountinfo] 2 mount=6 3 4 [v] 5 v1=14.2 6 v2=14.3 7 v3=14.4 8 v4=14.5 9 v5=14.6 10 v6=14.8 11 12 [a] 13 a1=2 14 a2=2.1 15 a3=2.2 16 a4=2.3 17 a5=2.4 18 a6=2.5 19 20 [p] 21 p1=126 22 p2=138 23 p3=139 24 p4=140 25 p5=141 26 p6=142 27 28 [j] 29 j1=4.8 30 j2=4.9 31 j3=4.7 32 j4=4.6 33 j5=4.5 34 j6=4.4
以上代码通过编译测试。
时间: 2024-11-03 21:43:46