I often need to calculate the sum of all values in a table column so I can display various summaries, averages etc.
Right now I'm using my own helper method:
double Utils::columnSum(QAbstractTableModel *model, int columnIndex)
{
Q_ASSERT_X(columnIndex >= 0 && columnIndex < model->columnCount(), "column sum", "column doesn't exist");
double sum = 0;
for (int ii = 0; ii < model->rowCount(); ++ii) {
auto val = model->data(model->index(ii, columnIndex));
bool ok;
sum += val.toDouble(&ok);
Q_ASSERT_X(val.isNull() || ok, "column sum", "column has non-numeric values");
}
return sum;
}
But if there is a framework provided function I'd rather use that, and knowing Qt, there probably is one somewhere but I couldn't find it.