[Qt-interest] metaobjectmodel, customtypes, lists
Thomas Ehrnhoefer
thomas.ehrnhoefer at tasktop.com
Sat Apr 2 00:45:27 CEST 2011
Hi
I am having some issues in creating a QVariantMap to Object conversion code
(for JSON deserialization).
Simple conversion works fine (populating my custom types using
QMetaProperty's write function).
However, when it comes to writing a property which in fact is a
QList<MyCustomType>, I am running into issues. My code compiles and the test
runs, but the property.write does not seem to actually write the correct
thing (in the example, the QList never seem to get set, it's empty even
though it should get set to a list with one item).
Does anybody know of an example that does this (even an example just doing
nested custom types, without even going into setting properties using the
MetaObjectModel would help)?
For completeness, here is what I am trying (in a simplified way):
====================================
===== function doing the deserialization =====
====================================
void MarshalUtil::mapToModel(QVariantMap map, QObject* model)
{
for (int i=0;i<model->metaObject()->propertyCount();i++)
{
QMetaProperty property = model->metaObject()->property(i);
if (map.contains(property.name()))
{
QString b (property.name());
if (map.value(property.name()).type() == QVariant::List)
{
if (property.isWritable())
{
QList<QVariant> list = map.value(property.name
()).toList();
QListIterator<QVariant> listIt(list);
if (b == "tasks")
{
QVariantList objectList;
while (listIt.hasNext())
{
QVariant variant = listIt.next();
if (variant.type() == QVariant::Map)
{
TaskHandle *taskHandle = new
TaskHandle(model);
mapToModel(variant.toMap(), taskHandle);
objectList.append(qVariantFromValue(*taskHandle));
}
}
property.write(model,
qVariantFromValue(objectList)); //this doesn't seem to work, even though
objectList has one entry
}
}
}
else
{
if (property.isWritable())
{
property.write(model, map.value(property.name()));
//this works just fine
}
}
}
}
}
========================
===== taskhandle.h =====
========================
#ifndef TASKHANDLE_H
#define TASKHANDLE_H
#include <QObject>
#include <QtCore>
#include <QMetaType>
class TaskHandle : public QObject
{
Q_OBJECT
Q_PROPERTY(QString taskId READ taskId WRITE setTaskId)
public:
TaskHandle();
TaskHandle(const TaskHandle &other);
TaskHandle(QObject *parent);
~TaskHandle();
public slots:
QString taskId() const;
void setTaskId(QString taskId);
private:
QString _taskId;
};
Q_DECLARE_METATYPE(TaskHandle);
#endif // TASKHANDLE_H
==========================
===== taskhandle.cpp =====
==========================
#include "taskhandle.h"
TaskHandle::TaskHandle()
{
}
TaskHandle::TaskHandle(const TaskHandle &other)
{
_taskId = other._taskId;
}
TaskHandle::TaskHandle(QObject *parent) : QObject(parent) {}
TaskHandle::~TaskHandle() {}
QString TaskHandle::taskId() const { return _taskId; }
void TaskHandle::setTaskId(QString taskId) { _taskId = taskId; }
===========================
===== taskcontainer.h =====
===========================
#ifndef TASKCONTAINER_H
#define TASKCONTAINER_H
#include <QObject>
#include <QMetaType>
#include "taskhandle.h"
class TaskContainer : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<TaskHandle> tasks READ tasks WRITE setTasks)
public:
TaskContainer();
TaskContainer(const TaskContainer &other);
TaskContainer(QObject *parent);
~TaskContainer();
public slots:
QList<TaskHandle> tasks() const;
void setTasks(QList<TaskHandle> tasks);
private:
QList<TaskHandle> _tasks;
};
Q_DECLARE_METATYPE(TaskContainer);
Q_DECLARE_METATYPE(QList<TaskHandle>);
#endif // TASKCONTAINER_H
=============================
===== taskcontainer.cpp =====
=============================
#include "taskcontainer.h"
TaskContainer::TaskContainer() {}
TaskContainer::TaskContainer(const TaskContainer &other)
{
_tasks = other._tasks;
}
TaskContainer::TaskContainer(QObject *parent) : QObject(parent) {}
TaskContainer::~TaskContainer() {}
QList<TaskHandle> TaskContainer::tasks() const { return _tasks; }
void TaskContainer::setTasks(QList<TaskHandle> tasks) { _tasks = tasks; }
--
Thomas Ehrnhoefer
Software Developer, http://tasktop.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110401/83ca806f/attachment.html
More information about the Qt-interest-old
mailing list