qt - How to store a QRect in Sqlite? -
i need store qrect in sqlite database. (i've searched around did not see on this)
does qt/qsqlite automatically convert qrect qvariant , handle whatever decomposition needed or have myself , store origin , size values in 4 separate fields?
if so, , generalize other qt data types, possible store qvector, qlist? define these (and qrect, etc.) blob types in table definitions?
sqlite relational database , things cannot store c++ objects directly.
actually c++ object model doesn't allow transparent persistence can try close using specialized tools or libraries. important point objects must designed support persistence.
if don't need transparent persistence explicit store/retrieval of objects can pick serialization method (e.g. using single string, or using separate fields attributes). each method has pros , cons depending on want database (e.g. kind of searches or updates want do).
something unfortunate c++ metaprogramming abilities poor (just little better c) , example introspection impossible.
this means need write serialization code each class need support or need find/buy external tool looking @ class definition .h
generate needed code you. must external tool because serialization outside limited reach of template metaprogramming.
qt has specialized serialization machinery qdatastream
may can use looking for: example qrect
serializable , qlist<t>
serializable if t
is.
using require serialize qbytearray
first , store blob inside sqlite. example after writing
template<typename t> qbytearray serialize(const t& x) { qbytearray ba; qdatastream s(&ba, qiodevice::writeonly); s << x; return ba; } template<typename t> t deserialize(qbytearray& ba) { qdatastream s(&ba, qiodevice::readonly); t res; s >> res; return res; }
you can serialize object directly qbytearray
with
qbytearray ba = serialize(x);
and can deserialize with
x x = deserialize<x>(ba);
Comments
Post a Comment