Reputation: 21
I'm trying to implement a qTableView structure.
Here's part of my code:
m_model = new PatientModel(this); //This is my QAbstractTableModel subclass
m_proxy = new QSortFilterProxyModel(this);
m_proxy->setSourceModel(m_model);
To append a row I say (I want to display patient objects):
void PatientModel::append(Patient* patient) {
beginInsertRows(QModelIndex(), m_data.count(), m_data.count());
m_data.append(patient);
endInsertRows();
}
Which works fine. The row gets added to the view and the data (m_data is a QList of
To remove a row I tried several things, at the moment I have this
bool PatientModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
this->layoutAboutToBeChanged();
beginRemoveRows(QModelIndex(), row, row+count-1);
m_data.removeAt(row);
endInsertRows();
this->layoutChanged(); //force refresh, keine Ahnung
return true;
}
I added the layoutAboutTobeChanged() and the layoutChanged() after some research. Before adding these 2 lines there was an empty row after deleting. Now there is not but when I remove line 3 for example I can't first click on Line 3+ anymore or the app will crash with the following error message:
QSortFilterProxyModel: index from wrong model passed to mapFromSource
Segmentation fault: 11
Is there anything I'm doing wrong?
Thanks in advance!
Upvotes: 2
Views: 1273
Reputation: 21
Nevermind, I guess I did something wrong. Changed RemoveRows to this and now it works:
bool PatientModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row+count-1);
for (int i=0; i < count; ++i) {
m_data.removeAt(row);
}
endRemoveRows();
return true;
}
Upvotes: 0