model view: How to tied up data form view with underlying data
-
Hello, I have some problem understanding model/view architecture, maybe is someone with free time to explain some things to me, please.
I have one table view activityView and all data inserted by the user in this table view is store into a c++ vector from another class( my own custom type, vector<activity>). I have edit, add and delete button. So i don't figure it out when user select one activity from the table to edit it how to tied up to the same activity from the stored data vector<activity>. At the moment i have an implementation with the same index(if user chose first row of the table, the first element of the vector is edited), it's not ok. I know it can be better but I don't know how.
Give me all links that you have and you think I need to read(I am here to learn). -
@AlexandruToma
No, you will impose sorting via aQSortFilterProxyModel
on top of your model if sorting is wanted. View will access that. Your indexes here will be unaffected. That is why a proxy model is nice :) -
Hi
So do you have your own custom model ?Normally in
QVariant Model::data(const QModelIndex &index, int role)
we use index.row take from the "source"Like here
https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.htmlwhere they expose a Contact class via a custom TableModel
-
Yes I have a custom model. And this is my data function
QVariant TableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole) {
return QVariant();
}const auto& teacher = mContext.GetTeacherByIndex(index.row());
switch (index.column()) {
case 0:
return QString::fromStdString((teacher->GetFirstName()));
case 1:
return QString::fromStdString((teacher->GetLastName()));
default:
break;
}
}The problem is: if i sort teachers by name in view, then would have different index.row(). So when i get teacher by index i would get another teacher
-
@AlexandruToma
No, you will impose sorting via aQSortFilterProxyModel
on top of your model if sorting is wanted. View will access that. Your indexes here will be unaffected. That is why a proxy model is nice :) -
This post is deleted!