Hi, everyone!
I am a noob in using model/view paradigm in Qt and have the following problem: I have a tree-like structure, that must be visualized via Qt. I found out, that QAbstractTableModel is perfect for my needs, so I write the following:
class columnViewModel : public QAbstractTableModel {
// some stuff...
};
Everything now works, but now I have to implement an "Observer" design pattern over the nodes of my tree. Whenever the node expands in the TreeView, I must add an Observer to the corresponding node. Whenever the node collapses, I must remove this Observer from the node. So, I write something, like this:
void onExpand( const QModelIndex & Index ... ) {
Node* myNode = static_cast<Node*>(Index->internalPointer());
Observer* foo = Observer::create();
myNode->addObserver(foo);
// ok up to here, but now where can I save this Observer? I must associate
// it with the Node, but I cannot change the Node class. Is there any way
// to save it within the index?
}
void onCollapse( const QModelIndex & Index ... ) {
Node* myNode = static_cast<Node*>Index->internalPointer();
Observer* foo = // well, what should I write here? Node does not have anything
// like "getObserver()"! Can I extract an Observer from Index?
myNode->remObserver( foo );
}
I don't have the snippets right now, so the code may be not a valid Qt, but the problem seems clear. I can change neither Node nor Observer classes. I can have a inner list of Observers, but then I have to resolve, what Observer to remove from the specific node. Is there any way to save the Observer pointer within Index (some user data maybe), to resolve it quickly in onCollapse? Any ideas would be welcome...