[odb-users] Persist container derived class as value type

Boris Kolpackov boris at codesynthesis.com
Mon Mar 7 09:03:43 EST 2016


Hi Henning,

Henning Becker <henning.becker at apworks.de> writes:

> I'm trying to persist QPolygon from Qt as a value type. QPolygon
> inherits from QVector<QPoint> and no data will be lost, if it is
> handled just as a QVector<QPoint>.
> 
> I have defined QPoint as a composite value type:
> #pragma db value(QPoint) transient
> #pragma db member(QPoint::x_) virtual(int) get(x) set(setX)
> #pragma db member(QPoint::y_) virtual(int) get(y) set(setY)
> 
> But how can I make odb handle QPolygon as a QVector<QPoint>?

There are two ways: "quick and dirty" and "slow but clean" ;-).

The "quick and dirty" approach is to map QPolygon to a composite value
with a QVector<QPoint> member:

using QPolygonBase = QVector<QPoint>;
#pragma db value(QPolygon) transient
#pragma db member(QPolygon::points) virtual(QPolygonBase) access(this)

class Object
{
  ...
  
  QPolygon perimeter;
};

The major drawback of this approach is that the database table name for
perimeter will be called "object_perimeter_points" and not just
"object_perimeter".

The "slow but clean" approach is to define an odb:container_traits<QPolygon>
specialization, similar to the one defined from QVector<T> (in fact, you can
just derive from it). See the Qt profile for details on how this is done.

Boris



More information about the odb-users mailing list