我有一个ini文件,如图所示
[Profiles]
hh\Ta=20
hh\Te=48
hh\Texp=38
hh\lim1=0
hh\lim2=0
hh\offset=0
hh\profilename=hh
tt\Ta=20
tt\Te=48
tt\Texp=38
tt\lim1=0
tt\lim2=0
tt\offset=0
tt\profilename=hh
我正在尝试加载它,但我将所有数据都作为零,如Ta Te Texp,等等。
文件已正确保存但重新加载值无法正常工作。
设置组是否有问题,等等?
这是我的加载和保存机制
void Method::loadFrom(Settings &set, bool ownGroup)
{
set.beginGroup("Profiles");
foreach (const QString &group, set.childGroups()) {
set.beginGroup(group);
Profile *profile = new Profile();
profile->setObjectName(group);
profile->loadFrom(set);
set.endGroup();
m_currentProfile->m_Profiles << profile;
}
set.endGroup();
EraObject::staticLoadFrom(set, this, ownGroup);
}
void Method::saveTo(Settings &set, bool ownGroup, bool force)
{
set.beginGroup("Profiles");
foreach(Profile * profile, m_currentProfile->m_Profiles) {
profile->saveTo(set);
set.endGroup();
}
set.sync();
EraObject::staticSaveTo(set, this, ownGroup, force);
}
class Settings : public QSettings
{
Q_OBJECT
public:
explicit Settings(QString fileName);
~Settings();
private:
Q_DISABLE_COPY(Settings)
};
void EraObject::staticLoadFrom(Settings &set, QObject *object, const bool &ownGroup)
{
Q_ASSERT(object);
bool modified;
const QMetaObject *superMeta = object->metaObject()->superClass();
const QMetaObject *meta = object->metaObject();
const QString &group = object->objectName().isEmpty() ? meta->className() : object->objectName();
if (ownGroup)
set.beginGroup(group);
qCDebug(loadLog) << group;
for (int i = superMeta->propertyOffset(); i < meta->propertyCount(); i++) {
modified = false;
const QMetaProperty &prop = meta->property(i);
QMetaType::Type type = (QMetaType::Type)prop.type();
if (prop.isUser()) {
switch (type) {
case QMetaType::QDateTime:
modified = setOrReset(set, object, prop, QDateTime::fromString(set.value(prop.name(), "").toString(), "yyyyMMddhhmmsszzz"));
break;
case QMetaType::QString: { //check if there is an localized string first (eg.: name@de=test)
const QVariant value = set.value(QString("%1@%2").arg(prop.name()).arg(QLocale::system().bcp47Name()),
set.value(prop.name(), ""));
modified = setOrReset(set, object, prop, value);
break;
}
case QVariant::UserType: {
QObject *obj = prop.read(object).value<QObject *>();
Q_ASSERT_X(obj, "staticLoadFrom", "Custom class NOT known to QMetaType, register it with qmlRegisterType()!");
bool check = QMetaObject::invokeMethod(obj,
"loadFrom",
Qt::DirectConnection,
Q_ARG(Settings &, set)); //we invoke cause it can be just QObject(e.g. QAbstractItemModel)
Q_ASSERT_X(check, "staticLoadFrom", "Could not invoke. Be sure you subclassed EraObject or defined method!: " + obj->objectName().toLatin1());
break;
}
default:
modified = setOrReset(set, object, prop, set.value(prop.name(), ""));
}
qCDebug(loadLog) << "modified:" << modified << "Property:" << prop.name();
}
}
if (ownGroup)
set.endGroup();
object->setProperty("dirty", NotDirty);
}
void EraObject::staticSaveTo(Settings &set, QObject *object, const bool &ownGroup, const bool &force)
{
Q_ASSERT(object);
if (force || object->property("dirty").toInt() == RamDirty) {
const QMetaObject *superMeta = object->metaObject()->superClass();
const QMetaObject *meta = object->metaObject();
const QString &group = object->objectName().isEmpty() ? meta->className() : object->objectName();
if (ownGroup)
set.beginGroup(group);
qCDebug(saveLog) << group;
for (int i = superMeta->propertyOffset(); i < meta->propertyCount(); i++) {
const QMetaProperty &prop = meta->property(i);
if (prop.isUser()) {
QMetaType::Type type = (QMetaType::Type)prop.type();
switch (type) {
case QMetaType::QDateTime:
set.setValue(prop.name(), prop.read(object).toDateTime().toString("yyyyMMddhhmmsszzz"));
break;
case QMetaType::Float:
case QMetaType::Double:
set.setValue(prop.name(), QString::number(prop.read(object).toDouble(), 'g', MAX_SAVED_DECIMALS));
break;
case QMetaType::User: {
QObject *obj = prop.read(object).value<QObject *>();
Q_ASSERT_X(obj, "staticLoadFrom", "Custom class NOT known to QMetaType, register it with qmlRegisterType()!");
bool check = QMetaObject::invokeMethod(obj,
"saveTo",
Qt::DirectConnection,
Q_ARG(Settings &, set),
Q_ARG(bool, true),
Q_ARG(bool, force));
Q_ASSERT_X(check, "saveTo", "Could not invoke. Be sure you subclassed EraObject or defined method!: " + obj->objectName().toLatin1());
break;
}
default:
if (!prop.isConstant())
set.setValue(prop.name(), prop.read(object));
}
}
}
if (ownGroup)
set.endGroup();
object->setProperty("dirty", NotDirty);
set.sync();
}
}