0

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).
andreahmed
  • 135
  • 1
  • 1
  • 9
  • why -1, please give a comment – andreahmed Apr 26 '16 at 13:22
  • `unit.value()` is a function. You need to write to a variable. – thuga Apr 26 '16 at 13:22
  • so make double value = unit.value then in >> value ? – andreahmed Apr 26 '16 at 13:23
  • If all your `Unit` class has is a single `qreal`, you might as well `typedef` it... – dtech Apr 26 '16 at 13:24
  • More like `double value; int >> value; unit.setValue(value);` – dtech Apr 26 '16 at 13:25
  • I'm assuming `unit.value()` returns some member variable. So either write directly to it, or do something like `qreal value; in >> value; unit.setValue(value);` – thuga Apr 26 '16 at 13:25
  • I still get [26.04 15:26:04 W] QVariant::save: unable to save type 'Unit*' (type id: 1359). – andreahmed Apr 26 '16 at 13:26
  • @thuga I'm trying to save a custom class and serialize it using qtsettings, I will show the loading and saving that I'm doing in the update code of the question – andreahmed Apr 26 '16 at 13:28
  • How many times do you plan on asking the same question in a row? You do hopefully realize this won't improve the odds of getting an answer. – dtech Apr 26 '16 at 13:29
  • @ddriver I have a different problem, that's why I asked the another question – andreahmed Apr 26 '16 at 13:31
  • @Murphy If it is a possible duplicate, did you provide an answer to the other question ? It is still open and I'm getting a lot of errors – andreahmed Apr 26 '16 at 13:31
  • Different problem - same question. If you get an extra problem, add that to the existing question instead of duplicating it. – dtech Apr 26 '16 at 13:33
  • @ddriver edited the old question. hope that you can help me to fix it :) – andreahmed Apr 26 '16 at 13:33
  • @andreahmed You could at least have made the subject lines different, including some hint about the kind of problem you're describing in the question. – Murphy Apr 26 '16 at 13:37
  • [26.04 15:27:37 W] QVariant::save: unable to save type 'Unit*' (type id: 1359). That's the error I'm getting – andreahmed Apr 26 '16 at 13:44
  • Posting multiple low-quality questions counts against you. Please don't do that. Work on fixing your original question until it can be answered. SO isn't an interactive debugging forum either - if the question can't be answered with small changes, it's off topic. – Kuba hasn't forgotten Monica Apr 26 '16 at 13:59

0 Answers0