QSettings settings("xxxx.ini",QSettings::IniFormat);
settings.setIniCodec(QTextCodec::codecForName("GB2312")); //在此添加设置,即可读写ini文件中的中文setup.ini文件内容:
[Rooms]
1RoomName=Room1
1PC1PCName=PC1
1PC1IP=192.168.1.121
1PC1App=C:\chess.exe
1PC1MACAddress=A0-2C-36-9E-94-FB
1PC2PCName=PC2
1PC2IP=192.168.1.152
1PC2App=D:\chess.exe
1PC2MACAddress=38-D5-47-00-1E-81
1PC3PCName=PC3
1PC3IP=192.168.1.153
1PC3App=D:\chess.exe
1PC3MACAddress=00-00-00-00-00-00
1PCsize=3
2RoomName=Room2
2PC1PCName=PC1
2PC1IP=192.168.1.121
2PC1App=C:\chess.exe
2PC1MACAddress=00-00-00-00-00-00
2PC2PCName=PC2
2PC2IP=192.168.1.152
2PC2App=D:\chess.exe
2PC2MACAddress=38-D5-47-00-1E-81
2PC3PCName=PC3
2PC3IP=192.168.1.153
2PC3App=D:\chess.exe
2PC3MACAddress=00-00-00-00-00-00
2PC4PCName=PC4
2PC4IP=192.168.1.154
2PC4App=D:\chess.exe
2PC3MACAddress=00-00-00-00-00-00
2PCsize=4
3RoomName=Room5
3PC1PCName=PC1
3PC1IP=192.168.1.121
3PC1App=C:\chess.exe|D:\A.exe|D:\B.exe|D:\C.exe
3PC1MACAddress=00-00-00-00-00-00
3PC2PCName=PC2
3PC2IP=192.168.1.152
3PC2App=D:\chess.exe|D:\A.exe|D:\B.exe|D:\C.exe
3PC2MACAddress=38-D5-47-00-1E-81
3PC3PCName=PC3
3PC3IP=192.168.1.153
3PC3App=D:\chess.exe|D:\A.exe|D:\B.exe|D:\C.exe
3PC3MACAddress=00-00-00-00-00-00
3PC4PCName=PC4
3PC4IP=192.168.1.154
3PC4App=D:\chess.exe|D:\A.exe|D:\B.exe|D:\C.exe
3PC4MACAddress=00-00-00-00-00-00
3PCsize=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();
*/