将类保存到qsettings中 [英] Saving a class into qsettings

查看:714
本文介绍了将类保存到qsettings中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类是一个QObject,它有一个成员是一个单位*。



储蓄抱怨它不能储存单位*

  [26.04 12:53:21 W] QVariant :: save:无法保存类型Unit *(类型id:1362)。 



我尝试用 qRegisterMetaTypeStreamOperators注册它
我得到以下错误

  / usr / include / qt / QtCore / qvariant.h:465:error:'QVariant :: QVariant(void *)'is private 
inline QVariant(void *)Q_DECL_EQ_DELETE;
^

这是我的加载和保存机制

  void Profile :: save(QSettings& settings)const {
for(int i = 0; i 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 const auto& p = metaObject() - > property(i);
if(p.isStored(this)){
setProperty(p.name(),settings.value(p.name()));
}
}
}

这里是我的单元类

  Unit :: Unit(int unit,QObject * parent)
:EraObject(parent),
m_value (0.0),
m_unit(unit)
{
connect(eApp-> systemController() - > unitManager(),& UnitManager :: onUnitChanged,this,& Unit: :changeUnit);
}

Unit :: Unit(const Unit& other)
{
m_value = other.m_value;
m_unit = other.m_unit;
qRegisterMetaTypeStreamOperators< unit *>(Unit *);
}

Unit& Unit :: operator =(const qreal value)
{
if(m_value == value)
return * this;

m_value = value;
emit userValueChanged();

return * this;
}


void Unit :: loadFrom(Settings& set,bool ownGroup)
{
Q_UNUSED(ownGroup)
if set.contains(objectName())){
m_value = set.value(objectName(),0.0).toDouble();
emit userValueChanged();
}
}

void Unit :: saveTo(Settings& set,bool ownGroup,bool force)
{
Q_UNUSED(ownGroup)
Q_UNUSED(force)
set.setValue(objectName(),m_value);
}

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 = (单位<< 16)。
setObjectName(name);
setParent(parent);
connectDirtyWithParent(parent);
}

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

UPDATE: b

现在我试着写

  QDataStream& operator<<(QDataStream& out,const Unit& v){
out< v.userValue();
return out;
}

QDataStream& operator>>(QDataStream& in,Unit& v){
in> v.userValue();
return in;
}

我得到

  /home/ahmed/server/elec/software_t/backup17/checko5/eracommon5/src/cpp/system/unit/unit.cpp:21:error:对于'operator< <'(操作数类型是'QDataStream'和'qreal(aka double}'))
out< v.userValue();
^

UPDATE2

  QDataStream& Unit :: operator<<(QDataStream& out,Unit& val)
{
out< ;& val.userValue();
return out;
}

QDataStream& Unit :: operator>>(QDataStream& out,Unit& val)
{
in> val.userValue();
return in;
}

更新3:
$ b

  Unit :: Unit(int unit,QObject * parent)
:EraObject(parent),
m_value b $ b m_unit(unit)
{
connect(eApp-> systemController() - > unitManager(),& UnitManager :: onUnitChanged,this,& Unit :: changeUnit);
qRegisterMetaTypeStreamOperators< Unit>(Unit);

}

Unit :: Unit(const Unit& other)
{
m_value = other.m_value;
m_unit = other.m_unit;
}


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


解决方案

href =http://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaTypeStreamOperators =nofollow> qRegisterMetaTypeStreamOperators() 。从那里开始修复您的流操作符,即它应该看起来像这样:

  QDataStream& operator<<(QDataStream& out,const Unit& unit); 
QDataStream& operator>>(QDataStream& out,Unit& unit);

否则,代码的那部分是正确的。



第二个修复是,您无法注册指针与 QMetaType 。您需要注册类型本身:

  qRegisterMetaTypeStreamOperators< Unit>(Unit); 

最后, qRegisterMetaType() c $ c> qRegisterMetaTypeStreamOperators()应该只调用一次(你已经调用 qRegisterMetaType()以及,是吗?如果您注意到 QMetaType :: load() ,这些函数应该只调用一次(例如在 int main()或一些其他初始化代码。


I have a class which is a QObject and it has a member which is a Unit *.

The saving is complaining that it can not save a Unit *

[26.04 12:53:21 W] QVariant::save: unable to save type 'Unit*' (type id: 1362).

I tried to register it with qRegisterMetaTypeStreamOperators<Unit*>("Unit*"); but I get the following error

/usr/include/qt/QtCore/qvariant.h:465: error: 'QVariant::QVariant(void*)' is private
     inline QVariant(void *) Q_DECL_EQ_DELETE;
            ^

Here is my loading and saving mechanism

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()));
        }
    }
}

here is my unit class

Unit::Unit(int unit, QObject *parent)
    : EraObject(parent),
      m_value(0.0),
      m_unit(unit)
{
    connect(eApp->systemController()->unitManager(),&UnitManager::onUnitChanged,this,&Unit::changeUnit);
}

Unit::Unit(const Unit &other)
{
    m_value = other.m_value;
    m_unit = other.m_unit;
    qRegisterMetaTypeStreamOperators<Unit*>("Unit*");
}

Unit &Unit::operator=(const qreal value)
{
    if (m_value == value)
        return *this;

    m_value = value;
    emit userValueChanged();

    return *this;
}


void Unit::loadFrom(Settings &set, bool ownGroup)
{
    Q_UNUSED(ownGroup)
    if(set.contains(objectName())) {
        m_value = set.value(objectName(),0.0).toDouble();
        emit userValueChanged();
    }
}

void Unit::saveTo(Settings &set, bool ownGroup, bool force)
{
    Q_UNUSED(ownGroup)
    Q_UNUSED(force)
    set.setValue(objectName(),m_value);
}

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();
}

UPDATE:

now I tried to write

QDataStream& operator<<(QDataStream& out, const Unit& v) {
    out << v.userValue();
    return out;
}

QDataStream& operator>>(QDataStream& in, Unit& v) {
    in >> v.userValue();
    return in;
}

and I get

/home/ahmed/server/elec/software_t/backup17/checko5/eracommon5/src/cpp/system/unit/unit.cpp:21: error: ambiguous overload for 'operator<<' (operand types are 'QDataStream' and 'qreal {aka double}')
     out << v.userValue();
         ^

UPDATE2 :

QDataStream &Unit::operator <<(QDataStream &out,  Unit &val)
{
    out << val.userValue();
    return out;
}

QDataStream &Unit::operator >>(QDataStream &out,  Unit &val)
{
    in >> val.userValue();
    return in;
}

Update 3:

Unit::Unit(int unit, QObject *parent)
    : EraObject(parent),
      m_value(0.0),
      m_unit(unit)
{
    connect(eApp->systemController()->unitManager(),&UnitManager::onUnitChanged,this,&Unit::changeUnit);
    qRegisterMetaTypeStreamOperators<Unit>("Unit");

}

Unit::Unit(const Unit &other)
{
    m_value = other.m_value;
    m_unit = other.m_unit;
}


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

解决方案

Here's the documentation for qRegisterMetaTypeStreamOperators(). Start there to fix your streaming operators, i.e. it should look like this:

QDataStream &operator<<(QDataStream &out, const Unit &unit);
QDataStream &operator>>(QDataStream &out, Unit &unit);

Otherwise, that portion of your code is correct.

The second fix is that you cannot register a pointer with QMetaType. You need to register the type itself:

qRegisterMetaTypeStreamOperators<Unit>("Unit");

Lastly, qRegisterMetaType() and qRegisterMetaTypeStreamOperators() should be called only once (you have called qRegisterMetaType() as well, right?). If you note the documentation for QMetaType::load(), these functions should only be called once (e.g. in int main() or some other initialization code.

这篇关于将类保存到qsettings中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆