I'm trying to save a custom class which is Unit * to qsettings.
first I register using qRegisterMetaTypeStreamOperators<Unit>("Unit");
then I wrote those two functions
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
now I get the following error
/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();
}
Profile code that saves the Unit *
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()));
}
}
}
so I'm trying to save a Unit* into the settings, I don't know where is the problem, but I get the following error
[26.04 15:27:37 W] QVariant::save: unable to save type 'Unit*' (type id: 1359).
After fixing the problem now I'm having the following functions
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;
}
but I'm still getting the following error
[26.04 15:27:37 W] QVariant::save: unable to save type 'Unit*' (type id: 1359).