簡體   English   中英

Qt QAbstractItemModel - 刪除項目崩潰

[英]Qt QAbstractItemModel - item removal crashes

我正在使用Qt類編寫應用程序,我有一個層次結構,我需要在樹視圖中顯示它(我使用的是QTreeView小部件)。 數據本身如下所示:

class StatisticsEntry
{
 // additional data management methods
 ...

bool setData(int column, const QVariant &value)
{
    if (column < 0 || column >= _data.size())
        return false;

    _data[column] = value;
    return true;
}

private:
    // each item has a parent item except the top-most one
    StatisticsEntry *_parent;
    // each item may have child items which are accessible through the following member
    QList<StatisticsEntry*> _children;
    // each item is defined by a set of vallues stored in the following vector
    QVector<QVariant> _data;
}

我有一個名為StatisticsModel的類,它實現了QAbstractItemModel - 用它來操作和呈現存儲在StatisticsEntry樹中的數據。 該類有一個名為addStatisticsData的方法,我用它將StatisticsEntry記錄推送到模型中。 該方法大致如下所示:

QModelIndex StatisticsModel::addStatisticsData(const QString &title, const QString &description, const QModelIndex &parent)
{
    int row = rowCount(parent);
    if (!insertRow(row, parent))
        return QModelIndex();

    // Get new item index
    QModelIndex child = index(row, 0, parent);

    // set item data
    setTitle(child, title);
    setDescription(child, description);

    return child;
}

SetTitle和setDescription方法是相同的 - 這是setTitle一個:

void StatisticsModel::setTitle(const QModelIndex &index, const QString& title)
{
    QModelIndex columnIndex = this->index(index.row(), StatColumn::Title, index.parent());
    this->setData(columnIndex, title, Qt::EditRole);
}

setData方法如下:

bool StatisticsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (role != Qt::EditRole)
        return false;

    int column = index.column();

    StatisticsEntry *item = getItem(index);
    if (!item->setData(column, value))
        return false;

    emit dataChanged(index, index);

    return true;
}

剩下的是getItem方法:

StatisticsEntry *StatisticsModel::getItem(const QModelIndex &index) const
{
    if (index.isValid())
    {
        StatisticsEntry *item = static_cast<StatisticsEntry*>(index.internalPointer());
        if (item)
            return item;
    }

    return _rootItem;
}

這是關於添加新的和修改現有條目的所有內容。 在我的應用程序中,我也實現了QSortFilterProxyModel - 沒有什么特別的,只有lessThan方法。 我使用代理模型在QTreeView中提供顯示數據的排序功能。 有以下代碼將模型連接到treeview小部件:

在主窗口的標題中:

...
StatisticsModel* _statisticsModel;
StatisticsProxyModel* _statisticsProxyModel;
...

在主寡婦的構造函數中

...
_statisticsModel = new StatisticsModel(ths);
_statisticsProxyModel = new StatisticsProxyModel(htis);
...
_statisticsProxyModel->setSourceModel(_statisticsModel);
ui->statisticsTreeView->setModel(_statisticsProxyModel);
...

該應用程序還有一個按鈕,允許我從模型中刪除所選項目 - 我目前只測試QTreeView :: selectionModel() - > currentIndex,暫時沒有多項選擇。

我有StatisticsModel :: removeRows方法的以下代碼

bool StatisticsModel::removeRows(int position, int rows, const QModelIndex &parent)
{
    StatisticsEntry *parentItem = getItem(parent);

    bool success1 = true;
    bool success2 = true;
    beginRemoveRows(parent, position, position + rows - 1);
    for (int i = position + rows - 1; i >= position; i--)
    {
        QModelIndex child = index(i, 0, parent);
        QString title = this->title(child); // the title method is the getter method that matches the setTitle one 
        success1 = success1 && removeRows(0, rowCount(child), child); //rowCount is implemented to return the number of items stored in StatisticsEntry::_children list for the specified parent index
        success2 = success2 && parentItem->removeChild(i); // deletes an entry from the StatisticsEntry::_children list
    }
    endRemoveRows();
    return success1 && success2;
}

問題是,有時當我使用QAbstractItemModel :: removeRow方法刪除項目時,我得到一個異常,堆棧跟蹤如下所示:

StatisticsModel::parent StatisticsModel.cpp 307 0x13e7bf8   
QModelIndex::parent qabstractitemmodel.h    393 0x72e57265  
QSortFilterProxyModelPrivate::source_to_proxy   qsortfilterproxymodel.cpp   386 0x711963e2  
QSortFilterProxyModel::mapFromSource    qsortfilterproxymodel.cpp   2514    0x7119d28b  
QSortFilterProxyModel::parent   qsortfilterproxymodel.cpp   1660    0x7119a32c  
QModelIndex::parent qabstractitemmodel.h    393 0x72e57265  
QPersistentModelIndex::parent   qabstractitemmodel.cpp  347 0x72e58b86  
QItemSelectionRange::isValid    qitemselectionmodel.h   132 0x70e3f62b  
QItemSelection::merge   qitemselectionmodel.cpp 466 0x711503d1  
QItemSelectionModelPrivate::finalize    qitemselectionmodel_p.h 92  0x7115809a  
QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved qitemselectionmodel.cpp 623 0x71151132  
QItemSelectionModel::qt_static_metacall moc_qitemselectionmodel.cpp 113 0x711561c2  
QMetaObject::activate   qobject.cpp 3547    0x72e8d9a4  
QAbstractItemModel::rowsAboutToBeRemoved    moc_qabstractitemmodel.cpp  204 0x72f08a76  
QAbstractItemModel::beginRemoveRows qabstractitemmodel.cpp  2471    0x72e5c53f  
QSortFilterProxyModelPrivate::remove_proxy_interval qsortfilterproxymodel.cpp   558 0x71196ce7  
QSortFilterProxyModelPrivate::remove_source_items   qsortfilterproxymodel.cpp   540 0x71196c7f  
QSortFilterProxyModelPrivate::source_items_about_to_be_removed  qsortfilterproxymodel.cpp   841 0x71197c77  
QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeRemoved qsortfilterproxymodel.cpp   1291    0x711995cc  
QSortFilterProxyModel::qt_static_metacall   moc_qsortfilterproxymodel.cpp   115 0x7119d506  
QMetaCallEvent::placeMetaCall   qobject.cpp 525 0x72e883fd  
QObject::event  qobject.cpp 1195    0x72e894ba  
QApplicationPrivate::notify_helper  qapplication.cpp    4550    0x709d710e  
QApplication::notify    qapplication.cpp    3932    0x709d4d87  
QCoreApplication::notifyInternal    qcoreapplication.cpp    876 0x72e6b091

奇怪的是,在關於物品移除的所有即時方法調用已經結束之后,這似乎發生了。 似乎代理模型正在尋找不再存在的模型索引(或者我認為)。 StatisticsModel :: parent方法如下:

QModelIndex StatisticsModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
        return QModelIndex();

    StatisticsEntry *childItem = getItem(index);
    StatisticsEntry *parentItem = childItem->parent();

    if (NULL != parentItem)
        return createIndex(parentItem->childNumber(), 0, parentItem);
    return QModelIndex();
}

當異常發生時,與上述方法中的childItem和parentItem變量關聯的值似乎無效 - 指針本身指向不可訪問的內存,或者成員QLists沒有條目或其條目給出內存訪問沖突。 可能是父方法不正確,但后來如何獲取父索引 - qabstractItemModel文檔不鼓勵在該方法中使用QModelIndex :: parent,因為它將創建無限遞歸。

任何幫助,將不勝感激,

在執行StatisticsModel::removeRows ,您正在嵌套begingRemoveRows/endRemoveRows ,這些AFAIK不起作用。 你應該做:

bool StatisticsModel::removeRows(int position, int rows, const QModelIndex &parent)
{
    StatisticsEntry *parentItem = getItem(parent);

    bool success1 = true;
    bool success2 = true;
    for (int i = position + rows - 1; i >= position; i--)
    {
        QModelIndex child = index(i, 0, parent);
        QString title = this->title(child); // the title method is the getter method that matches the setTitle one 
        success1 = success1 && removeRows(0, rowCount(child), child); //rowCount is implemented to return the number of items stored in StatisticsEntry::_children list for the specified parent index
        beginRemoveRows(parent, i, i);
        success2 = success2 && parentItem->removeChild(i); // deletes an entry from the StatisticsEntry::_children list
        endRemoveRows();
    }
    return success1 && success2;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM