QStandardItemModel:添加行的任何有效方法?

QStandardItemModel: any efficient way to add rows?

我已经尝试使用 Qt void QStandardItem::insertRow(int row, const QList<QStandardItem *> &items)void QStandardItem::appendRow(const QList<QStandardItem *> &items) 在我的模型中动态添加行。对于少量行,这些花费的时间非常少。但是,对于大量的行条目,比如 100,000,这需要很长时间。

I read this similar question 但帮助不大。有没有其他方法可以更有效地做到这一点?

感谢评论区给我指明了正确的方向,我才能够自己解决问题。

我试图实现 QAbstractItemModel 的子类。下面给出的是 bool QAbstractItemModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) 的含义。此代码只是在我的 GUI 中添加了空白单元格。这个想法只是为了检查细胞添加的速度:

bool CustomTableModel::insertRows(int position, int rows, const QModelIndex &parent)
{
    beginInsertRows(parent, position, position + rows - 1);
    for (int row = 0; row < rows; row++) 
    {
        QStringList items;
        for (int column = 0; column < 7; ++column)// I required only 7 columns 
            items.append("");
        rowList.insert(position, items); // QList<QStringList> rowList;
    }
    endInsertRows();
    return true;
}

这种方法提高了添加新行的整体性能。但是,对于我的要求来说,它仍然不是很快。似乎 QAbstractItemModel::beginInsertRows(const QModelIndex & parent, int first, int last)QAbstractItemModel::endInsertRows() 造成了整体瓶颈。

最后,我只是使用以下构造函数创建了一个 table 足够大的行数:

CustomTableModel::CustomTableModel(int rows, int columns, QObject *parent): QAbstractTableModel(parent)
{
    QStringList newList;

        for (int column = 0; column < columns; column++) {
            newList.append("");
        }

        for (int row = 0; row < rows; row++) {
            rowList.append(newList); // QList<QStringList> rowList;
        }
}

然后我创建了一个自定义函数来将值插入到单元格中:

void CustomTableModel::insertRowValues(int row,int col, QString val)
{
    rowList[row][col] = val;
}

重复调用此函数以填充单个单元格创建 table 出奇的快(或至少比之前更快)。这个方案感觉不是很优雅,但是解决了我的问题