I'm trying to develop a class that will be used in a reflection driven application and I ran into an interesting issue that I'm hoping there is a solution to. Let's say I have a class Dummy that has a few properties, one of which is a pointer to another Dummy object:
{
Q_OBJECT
Q_PROPERTY(int id READ id WRITE setId)
Q_PROPERTY(QObject * child READ child
)
int myId;
DummyObject *myChild;
public:
Dummy(int id)
: myId(id), myChild(0)
{
if (myId == 0)
{
myChild = new Dummy(myId + 1);
}
}
~Dummy()
{
delete myChild;
}
int id() const
{
return myId;
}
void setId(int id)
{
myId = id;
}
{
return myChild;
}
};
class Dummy : public QObject
{
Q_OBJECT
Q_PROPERTY(int id READ id WRITE setId)
Q_PROPERTY(QObject * child READ child)
int myId;
DummyObject *myChild;
public:
Dummy(int id)
: myId(id), myChild(0)
{
if (myId == 0)
{
myChild = new Dummy(myId + 1);
}
}
~Dummy()
{
delete myChild;
}
int id() const
{
return myId;
}
void setId(int id)
{
myId = id;
}
QObject * child()
{
return myChild;
}
};
To copy to clipboard, switch view to plain text mode
And then lets say I create a new Dummy object with id = 0 (so it has one child with id = 1), if I pass the parent Dummy object to a function that takes a constant reference to a QObject (or a constant pointer for that matter), how can I guarantee const-correctness on the child function? I tried the obvious: Q_PROPERTY(const QObject * child READ child) and then making const QObject * child() const (which is really how I want this function to look like), but then the moc complains about: "Unable to handle unregistered datatype 'const QObject*'". I just can't figure out how to make it so that if I pass a constant reference or pointer to a Dummy object that the "child" function (through the qt property system) will expose a constant reference or pointer (to ensure const-correctness).
Bookmarks