Model View with nested lists
-
Hi,
I have some rather simple data available as nested std::vectors, which I want to show in two list views. Although this could be represented as a tree, it strikes me as overkill, as
Parent
andChild
do not share a common base class. So my question is, how would this structure best implemented in the Model View framework provided by Qt? Two nested list models are rather hard to synchronize, imposing a common base class on the data feels unnatural to me.The rough structure in code is as follows:
class Child { // Some boring data with accessors and stuff }; class Parent { public: Foo(QString name); QString name() const; const std::vector<std::shared_ptr<Bar>> &entries(); // some accessors and stuff private: QString m_name; std::vector<std::shared_ptr<Bar>> m_entries; }; std::vector<std::shared_ptr<Parent>> getData() { // returns a vector of ``Parent`` pointers, which in turn contain vectors of ``Child`` pointers }
and a simple UI mockup:
-
Hi and welcome to devnet,
What are you going to show from Child in your view ?
-
What are you going to show from Child in your view ?
The data contained in
Child
class, which consists mostly ofQString
. -
So you are going to show in the second list view the child content from the parent select in the first view ?
-
So you are going to show in the second list view the child content from the parent select in the first view ?
Yes, I thought about the following structure:
├ Parent0 (selected in List View 1) │ ├ Child (shown in List View 2) │ ├ Child (selected in List View 2) │ │ └ Contents of Child shown in the lower area │ └ Child (shown in List View 2) ├ Parent (shown in List View 1) │ ├ Child │ └ Child └ Parent (shown in List View 1) └ Child
It looks like a tree, but the question is how would I impose this tree structure onto the model classes. The simple tree model example uses an actual tree to store the data, which is not the case in my situation.
-
Technically you have a two-level tree.
You can still use that example as a base. In place of their custom TreeViewItem you have your Parent and Child.
Those the abstract model would be a wrapper around your Parent/Child combo.