我有一个QObject类,它有一个Unit *。
保存是抱怨它无法保存单位*
[26.04 12:53:21 W] QVariant::save: unable to save type 'Unit*' (type id: 1362).
我尝试将其注册到qRegisterMetaTypeStreamOperators<Unit*>("Unit*");
但是
我收到以下错误
/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<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::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();
}
更新
现在我试着写
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: 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;
}
更新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;
}
答案 0 :(得分:0)
这是qRegisterMetaTypeStreamOperators()
的文档。从那里开始修复您的流媒体运营商,即它应该如下所示:
QDataStream &operator<<(QDataStream &out, const Unit &unit);
QDataStream &operator>>(QDataStream &out, Unit &unit);
否则,您的代码部分是正确的。
第二个问题是您无法使用QMetaType
注册指针。您需要注册类型本身:
qRegisterMetaTypeStreamOperators<Unit>("Unit");
最后,qRegisterMetaType()
和qRegisterMetaTypeStreamOperators()
只应调用一次(您也调用了qRegisterMetaType()
,对吗?)。如果您注意QMetaType::load()
的文档,则只应调用一次这些函数(例如,在int main()
或其他一些初始化代码中。