Ok I found one way to do this.
I created a parent type called CustomType with a virtual method that I can implement to convert my custom type to a "normal" QVariant:
class CustomType {
public:
virtual ~CustomType() {}
virtual QVariant toVariant() const { return QVariant(); }
};
I then inherited my custom Money class from this.
class Money : public CustomType {
public:
Money(double d) {
_value = d;
}
~Money() {}
QVariant toVariant() {
return QVariant(_value);
}
private:
double _value;
};
This allows me to pass my custom Money variables contained in QVariants so I can use them in the Qt property system, model/view framework, or the sql module.
But if i need to store my custom Money variable in the database (using QSqlQuery.addBindValue) it can't be a custom class, it has to be a known type (like double).
QVariant myMoneyVariant = myqobject.property("myMoneyProperty");
void *myData = myMoneyVariant.data();
CustomType *custType = static_cast<CustomType*>(myData);
QVariant myNewVariant = ct->toVariant();
myNewVariant now has the type of double, not Money so I can use it in a database:
myqsqlquery.addBindValue(myNewVariant);
or convert it to a string:
QString s = myNewVariant.toString();