I'm trying to store enum in SQLite database using QSql. I have following class:
partydao.h:
class PartyDao : public QObject
{
public:
enum partyType { typeCompany, typePerson };
private:
Q_OBJECT
Q_ENUMS(partyType)
Q_PROPERTY(partyType type READ type WRITE set_type)
// other declarations
};
Q_DECLARE_METATYPE(PartyDao::partyType)
partydao.cpp:
#include "partydao.h"
static int id = qRegisterMetaType<PartyDao::partyType>("partyType");
I do insert like this:
PartyDao p;
p.setProperty("type", QVariant::fromValue(PartyDao::typePerson));
QSqlQuery query;
query.prepare("INSERT INTO party (type) values (:type)");
qDebug() << p.property("type").isNull();
query.bindValue(":type", p.property("type"));
query.exec();
Although qDebug() prints "false" (i.e. property is not null), null value is stored in db.
I've tried with column of type TEXT and INTEGER with no success.
Can you tell me what I'm doing wrong?
EDIT:
I've just checked and QVariant holding my enum claims it can't be converted to QString or int (canConvert<int>() and canConvert<QString>() return false). Is there a way to add this conversion?