Grid View with 2 models
-
wrote on 13 Dec 2023, 03:19 last edited by
I have a Grid View that needs to display 2 different delegates (with different roles), both representing different C++ objects, A and B. A and B do have some common methods, so I was thinking I could subclass them from the same base class and have a QList<Base*> in a single model, combining both object types. I could then do a dynamic_cast in data() to get the subclassed object, and return the correct role, etc.
Alternatively, is there a way to display data from 2 different models in a single GridView? Although there is some common functionality between A and B that may justify inheritance, there is no interplay between the 2, and combining them into a single model would only be for the sake of displaying them in a single view. Could I achieve something like this with Loaders?
-
I have a Grid View that needs to display 2 different delegates (with different roles), both representing different C++ objects, A and B. A and B do have some common methods, so I was thinking I could subclass them from the same base class and have a QList<Base*> in a single model, combining both object types. I could then do a dynamic_cast in data() to get the subclassed object, and return the correct role, etc.
Alternatively, is there a way to display data from 2 different models in a single GridView? Although there is some common functionality between A and B that may justify inheritance, there is no interplay between the 2, and combining them into a single model would only be for the sake of displaying them in a single view. Could I achieve something like this with Loaders?
@ECEC both your solutions are OK.
If you want to use something more automated, you can use QConcatenateTablesProxyModel.
-
wrote on 13 Dec 2023, 09:06 last edited by
I don't understand the question. It appears to be conflating delegates (visual representation) with models or data within a model. This is a common in my experience, including with widgets users that are accustomed to usable default delegates. How does 2 different delegates (via DelegateChooser or a homegrown implementation) for one model map to two models with one delegate?
Code will likely make the question easier to understand. Pseudocode or a toy program is fine.
-
@ECEC both your solutions are OK.
If you want to use something more automated, you can use QConcatenateTablesProxyModel.
wrote on 14 Dec 2023, 21:43 last edited byPerhaps an even better way would be to maintain 2 separate QLists for A and B within the same model and use their sizes and the QModelIndex in data() to retrieve the correct object. That way I'm not trying to force polymorphism where it doesn't belong and don't have to mess around with Loaders. Which of these 3 options do you think is best?
1/4