I understand how to expose simple data members by creating a new QObject
class that has my class as a member. For example:
class MyClass {
public:
int anInt;
};
And the wrapper
class MyClassQWrapped : public QObject {
Q_OBJECT
Q_PROPERTY(int anInt READ anInt write setAnInt NOTIFY anIntChanged)
public:
int anInt(){return myClass.anInt;}
void setAnInt(int value){
if(myClass.anInt==value)return;
myClass.anInt = value;
emit anIntChanged;
}
signals:
anIntChanged();
private:
MyClass myClass;
};
But how do I wrap objects with pointer data members? Say for instance I had a linked list class, and wanted to expose it to qml:
Class LinkedList {
public:
int anInt;
LinkedList *next;
};
I can't use the same approach as above, because then the property of the wrapper would be a pointer to LinkedList
, not to LinkedListQWrapped
. If the list is changed by non-Qt code, I have no way of knowing which (if any) LinkedListQWrapped
object correspond to a given LinkedList*
.
Now, I'm allowed to change the c++ model, but I can't let it depend on Qt.
I can think of one solution where I add a void*
user data member to the c++ class and then set it to point to the corresponding wrapped object once I've created the wrapper.
I've seen this sort of thing done in box2d.
Is this a recommended way to solve the problem? Is it common to provide void*
user data members in library code to make them play nice with various frameworks such as Qt? Or is there another solution to this problem?