环境为 window7 ,qt4.4.0,mingw,eclipse
ini文件:
[title]
logname = "test1"
mainname = "test2"
helpname = "test3"
读取方法为:
QSettings *config=new QSettings(QObject::tr("name.ini"),QSettings::IniFormat);
config->beginGroup(QObject::tr("title"));
QString logname=config->value("logname").toString();
QString mainname=config->value("mainname").toString();
//QMessageBox::information(NULL, QObject::tr("管理中心"),logname);
//QMessageBox::information(NULL, QObject::tr("管理中心"),mainname);
不知道为什么logname 和 mainname 中总是为空 ,请高手指教,谢谢
------解决方案--------------------
也有可能是编码问题:
QSettings *pSettings = new QSettings(INI_PATH , QSettings::IniFormat);
pSettings->setIniCodec(QTextCodec::codecForName("gb18030"));
//加上这行选用合适的编码然后再利用value()读取也许能解决问题
------解决方案--------------------
实在不行直接用fscanf得了
char name1[20];
char name2[20];
char name3[20];
FILE *ini= fopen("name.ini","r");
fscanf(ini,"logname = %s",name1);
……
如果你的logname是放在""里的,可以改成fscanf(ini,"logname = \"%s\"",name1);
如果是存储程序设置,没有必要自己手动去读文件吧?Qt自带有这种机制,可以对应相应的系统自动使用一种保存方式。我写过的一个Settings:
直接使用QSettings的setValue设定值
用value读取,已经有了相应的键表了,就不需要自己手动提取了。
void ConfigInfo::loadConfig()
{
QSettings settings(SETTING_NAME,VERSION);
edu=settings.value("Edu",edu).toBool();
autoHide=settings.value("AutoHide",autoHide).toBool();
remember=settings.value("Remember",remember).toBool();
autoConnect=settings.value("AutoConnect",autoConnect).toBool();
autoRetry=settings.value("AutoRetry",autoRetry).toBool();
exitShowNextTime=settings.value("ExitShowNextTime",exitShowNextTime).toBool();
retryTimes=settings.value("RetryNumber",retryTimes).toInt();
responseTimeOut=settings.value("MaxServerResponseTime",this->responseTimeOut).toInt();
device =this->interface;
device=settings.value("Device",device).toString();
strcpy(interface,device.toAscii());
refreshTimeOut=settings.value("WaitTime",this->refreshTimeOut).toInt();
name_=settings.value("Account",name_).toString();
password_=settings.value("Password",password_).toString();
strcpy(this->name,name_.toAscii());
strcpy(this->password,password_.toAscii());
}
void ConfigInfo::loadDefault()
{
this->autoRetry=true;
this->retryTimes=3;
this->responseTimeOut=10;
this->refreshTimeOut=180;
strcpy(this->interface,"eth0\0");