简体   繁体   中英

QTableView: Removing Row from Model -> Empty row and “QSortFilterProxyModel: index from wrong model passed to mapFromSource”

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!

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM