last day (15 days later) » 

15:45
0
A: Leveraging Qt models for nested data structures

CalethI'd be tempted to have all the data in one model per type, as QAbstractTableModel1 with a column per data member, and then use QSortFilterProxyModels for each view to filter what data that view sees. The first screen would be three views each showing just the name from each model, and selecting a...

I had considered a QAbstractTableModel, however the Employee, Department, and Store classes have additional responsibilities and decoupling the data from the functions doesn't align that well with OOP principles.
@19172281 Don't think of QT's model classes as models in the MVC sense. They are more like viewmodels in the MVVM sense.
@19172281 you still have your Employee, Department and Store classes, but you populate EmployeeModel, DepartmentModel and StoreModel as flat tables at the top level
So then I'd need some mechanism by which to synchronise between the Employee, Department and Store classes, and their models?
@19172281 you can override QAbstractItemModel::data and QAbstractItemModel::setData to interact with your classes, but you still have to deal with adding / removing / moving rows when the underlying list changes shape, yes
Would I still have my existing hierarchy? I.e. manager own a QVector of Store, each Store owns a QVector of Department, etc
15:45
@19172281 yes, those are separate from the models
What would each flat model contain? Surely we would know what entry belongs to what parents when it comes to filtering?
@19172281 parent and grandparent for EmployeeModel, yes
Would that be an index for the parent/grandparent, or pointers? It just seems that with your approach we'd need to maintain the hierarchy in 2 places while still having multiple models.
@19172281 I'd use the id's. Each filter model will override QSortFilterProxyModel::filterAcceptsColumn to hide columns it doesn't use, and QSortFilterProxyModel::filterAcceptsRow to hide rows based on ids (that it can store)
I see. Would you still make any changes to data only via the manager class? I do worry about models falling out of sync with one another.
15:45
Your overload of QAbstractItemModel::setData can call the manager class. You don't copy the data. The model looks up the data each time it needs it
Ah, no object references at all. Just indexes to find the data via manager? E.g. for Employee, an entry might be something like, 3,1,7. Which gives us the 7th Employee of the 1st Department of the 3rd Store.
Something like godbolt.org/z/xM9absrWv
I would prefer unique id's for every entity, rather than pointers for setEmployeeName et. al.
16:28
struct EmployeeInfo {
QString storeId;
QString departmentId;
Employee* employee;
};

But you still have a pointer to the employee, where you could have an index
It can reasonably be a const pointer, you aren't going to be modifying through it
Pointer is just easier than traversing the hierarchy each time
16:49
 
4 hours later…
21:03
Thanks for all your help with this. I think it's clear now!

last day (15 days later) »