【问题标题】:The amount of items in QTableView stays the same after changing the model更改模型后 QTableView 中的项目数量保持不变
【发布时间】:2012-06-04 07:44:41
【问题描述】:

我有一个具有以下模型的虚拟表视图实现:

class MyModel: public QAbstractListModel
{

    int columnCount (const QModelIndex & parent = QModelIndex() ) const { return 2; }
    int rowCount (const QModelIndex & parent = QModelIndex() ) const { return count; }
    QModelIndex parent (const QModelIndex & index ) const { return QModelIndex(); }
    QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex() ) const { return createIndex(row, column); }


QVariant data(const QModelIndex & index, int role) const
{
    int col = index.column();
    int row = index.row();

    if (role == Qt::DecorationRole && col == 0)
    {            
        return getIcon(row); // icons in the first column
    }
    else if (role == Qt::DisplayRole && col == 1)
    {
        return getText(row); // text in the second column            
    }
    else
    {
        return QVariant();
    }
}

void update()
{
  getNewText();
  getNewIcons();  
  emit dataChanged((index(0,0)), index(count-1,1));
}

}

第一次创建表格视图并分配模型后,一切正常:比如说,表格视图中有 10 个项目。

但后来我更新了模型,现在它有 12 个项目。仅显示其中的前 10 个。看起来它缓存了 10 的值,不想更新它。

我该如何解决这个问题?

【问题讨论】:

    标签: c++ qt view model


    【解决方案1】:

    我通过在update方法中调用beginRemoveRowsendRemoveRowsbeginInsertRowsendInsertRows解决了这个问题

    【讨论】:

    • 就像 Qt 文档清楚地告诉你的那样,我可能会补充:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多