Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. can't load properties from a class using qsettings

can't load properties from a class using qsettings

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 597 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndreAhmed
    wrote on last edited by
    #1

    I'm trying to save and load a class using the following functions in Qt

    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();
        }
    }
    Now I try to save and load the class using the following methods
    
    void Method::loadFrom(Settings &set, bool ownGroup)
    {
        Settings s("profiles.ini");
        s.beginGroup("Profiles");
       // s.beginGroup(m_filename);
        foreach (const QString &group, s.childGroups()) {
            if(group == " Default")
                continue;
            s.beginGroup(group);
            Profile *profile = new Profile();
            profile->setObjectName(group);
            profile->loadFrom(s);
            s.endGroup();
    
            m_currentProfile->m_Profiles << profile;
        }
        s.endGroup();
    
        EraObject::staticLoadFrom(set, this, ownGroup);
    
    }
    
    void Method::saveTo(Settings &set, bool ownGroup, bool force)
    {
        Settings s("profiles.ini");
        s.beginGroup("Profiles");
        //s.beginGroup(m_filename);
        foreach(Profile * profile, m_currentProfile->m_Profiles) {
          //  s.beginGroup(profile->profilename());
            profile->saveTo(s);
           // s.endGroup();
        }
        s.endGroup();
        s.sync();
    
        EraObject::staticSaveTo(set, this, ownGroup, force);
    
    }
    

    The class can be saved correctly and that's the result in the ini file

    
    [Profiles]
    jjk\Ta=20
    jjk\Te=48
    jjk\Texp=38
    jjk\lim1=0
    jjk\lim2=0
    jjk\offset=0
    jjk\profilename=jjk
    tyy\Ta=20
    tyy\Te=48
    tyy\Texp=38
    tyy\lim1=0
    tyy\lim2=0
    tyy\offset=0
    tyy\profilename=tyy
    

    The problem is the loaded values are all zeros and it doesn't load Ta, Te, Texp..etc.

    I don't know where is the problem.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but are you sure you are entering loadFrom and the loop in it ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved