将自定义类保存到qsettings中

时间:2016-04-26 13:21:15

标签: c++ qt qt5

我正在尝试将一个自定义类(单位*)保存到qsettings。

首先我使用qRegisterMetaTypeStreamOperators<Unit>("Unit");

进行注册 然后我写了这两个函数

QDataStream &operator<<(QDataStream &out, const Unit &unit)
{
     out << unit.value();
     return out;
}

QDataStream &operator>>(QDataStream &in, Unit &unit)
{
    in >> unit.value();
    return in;

}
unit.value() returns a qreal 

现在我收到以下错误

/home/ahmed/server/elec/software_t/backup17/checko5/eracommon5/src/cpp/system/unit/unit.cpp:57: error: no match for 'operator>>' (operand types are 'QDataStream' and 'qreal {aka double}')
     in >> unit.value();

    ^



/home/ahmed/server/elec/software_t/backup17/checko5/eracommon5/src/cpp/system/unit/unit.cpp:57: error: invalid initialization of non-const reference of type 'qint8& {aka signed char&}' from an rvalue of type 'qint8 {aka signed char}'
     in >> unit.value();
                     ^


**Update:**
    QDataStream &operator<<(QDataStream &out, const Unit &unit)
{
     out << unit.value();
     return out;
}
QDataStream &operator>>(QDataStream &in, Unit &unit)
{
    double value;
    in >> value;
    unit.setUserValue(value);
    return in;
}


Unit::operator qreal() const
{
    return m_value;
}

qreal Unit::userValue() const
{
    return isDefault() ?  m_value : UnitManager::convertTo(m_value,m_unit);
}

QString Unit::symbol() const
{
    return UnitManager::symbolName(m_unit);
}

void Unit::setUserValue(const qreal userValue)
{
    qDebug() << "setUserValue" <<  this->userValue() << userValue << QString::number(m_unit,2);
    if (this->userValue() == userValue)
        return;

    if(isDefault())
        m_value = userValue;
    else
        m_value = UnitManager::convertFrom(userValue,m_unit);

    qDebug() << "Value" <<  m_value;

    emit userValueChanged();
    setDirty(RamDirty);
}

void Unit::setup(quint32 unit, const QString name, QObject *parent)
{
    if(!m_unit)
        m_unit = (unit << 16);
    setObjectName(name);
    setParent(parent);
    connectDirtyWithParent(parent);
}

void Unit::changeUnit(const quint32 &unit)
{
    if(m_unit == unit || category() != (unit >> 16))
        return;
    m_unit = unit;
    emit userValueChanged();
    emit symbolChanged();
}

保存单位*的个人资料代码

void Profile::save(QSettings& settings) const {
    for(int i=0; i<metaObject()->propertyCount(); ++i) {
        const auto& p = metaObject()->property(i);
        if(p.isStored(this)) {
            settings.setValue(p.name(), property(p.name()));
        }
    }
}

void Profile::load(QSettings& settings) {
    for(int i=0; i<metaObject()->propertyCount(); ++i) {
        const auto& p = metaObject()->property(i);
        if(p.isStored(this)) {
            setProperty(p.name(), settings.value(p.name()));
        }
    }
}

所以我试图将Unit *保存到设置中,我不知道问题出在哪里,但是我收到以下错误

[26.04 15:27:37 W] QVariant::save: unable to save type 'Unit*' (type id: 1359).

现在解决问题之后,我有以下功能

QDataStream &operator<<(QDataStream &out, const Unit &unit)
{
     out << unit.value();
     return out;
}

QDataStream &operator>>(QDataStream &in, Unit &unit)
{
    double value;
    in >> value;
    unit.setUserValue(value);
    return in;
}

但我仍然收到以下错误

[26.04 15:27:37 W] QVariant::save: unable to save type 'Unit*' (type id: 1359).

0 个答案:

没有答案