1

I am getting an out of memory crash when using QTableView + QAbstractTableModel with a large number of items.

The problem seems to come from the vertical header view. It tries to allocate a QVector with same size as the row count (150 millions).

How can i use millions of rows in my model without crashing the table?

Here is a sample to reproduce the issue.

class MiniModel : public QAbstractTableModel
{
Q_OBJECT

public:
    MiniModel(QObject * parent = nullptr);
    int columnCount(QModelIndex const &parent = QModelIndex()) const override;
    int rowCount(QModelIndex const &parent = QModelIndex()) const override;
    QVariant data(QModelIndex const &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
};

MiniModel::MiniModel(QObject * parent) : QAbstractTableModel(parent)
{
}

int MiniModel::columnCount(QModelIndex const & parent) const
{
    return 1;
}

int MiniModel::rowCount(QModelIndex const & parent) const
{
    return 150000000;
}

QVariant MiniModel::data(QModelIndex const & index, int role) const
{
    if (role == Qt::DisplayRole)
        return 23; // just return 23 in all cells
    else
        return QVariant();
}

QVariant MiniModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (section == 0 && role == Qt::DisplayRole && orientation == Qt::Horizontal)
        return "Dummy";
    else
        return QVariant();
}
Octo
  • 543
  • 1
  • 5
  • 16

0 Answers0