【问题标题】:QTableView: Removing Row from Model -> Empty row and "QSortFilterProxyModel: index from wrong model passed to mapFromSource"QTableView:从模型中删除行 - >空行和“QSortFilterProxyModel:来自错误模型的索引传递给mapFromSource”
【发布时间】:2019-04-25 02:59:11
【问题描述】:

我正在尝试实现一个 qTableView 结构。

这是我的部分代码:

m_model = new PatientModel(this); //This is my QAbstractTableModel subclass
m_proxy = new QSortFilterProxyModel(this);
m_proxy->setSourceModel(m_model);

要附加一行我说(我想显示患者对象):

void PatientModel::append(Patient* patient) {
   beginInsertRows(QModelIndex(), m_data.count(), m_data.count());
        m_data.append(patient);
   endInsertRows();
}

效果很好。该行被添加到视图和数据中(m_data 是

的 QList

要删除一行我尝试了几件事,目前我有这个

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

经过一番研究,我添加了 layoutAboutTobeChanged() 和 layoutChanged()。在添加这两行之前,删除后有一个空行。现在没有,但是当我删除第 3 行时,例如我不能先点击第 3+ 行,否则应用程序将崩溃并显示以下错误消息:

QSortFilterProxyModel: index from wrong model passed to mapFromSource
Segmentation fault: 11

是不是我做错了什么?

提前致谢!

【问题讨论】:

    标签: c++ qt viewmodel qtableview qsortfilterproxymodel


    【解决方案1】:

    没关系,我想我做错了什么。将 RemoveRows 更改为此,现在它可以工作了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2014-04-11
      相关资源
      最近更新 更多