PDA

View Full Version : calculated columns for QSqlRelationalTableModel



schnitzel
23rd October 2009, 06:05
I'm working on an app for a MySQL db. One of the tables features 3 columns that I'd like to sum up per row and then a subtotal for all rows for those 3 columns.
I managed to get the column subtotals, but for the row total (for the 3 fields of that particular row), I would need a calculated field.

After googling, the only reference I could find is this thread:
http://lists.trolltech.com/qt-interest/2008-02/thread00142-0.html

would that be the best way to approach this or is it possible to change the underlying SQL for the QSqlRelationalTableModel?

cheers,
-A

schnitzel
24th October 2009, 05:52
I tried what was suggested in the link, but it doesn't work, i.e. the newly calculated row doesn't show, even though I inserted the new column in my instance of my custom model (based on QSqlRelationalTableModel).

my data method looks like this:



QVariant CustomModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlRelationalTableModel::data(index,role);

if(role == Qt::TextAlignmentRole && (index.column() == 4 || index.column() ==5 || index.column() == 6))
value = QVariant(Qt::AlignRight | Qt::AlignVCenter);
else if(role == Qt::DisplayRole && index.column() == 7)
{
QModelIndex mi = this->index(index.row(),4,QModelIndex());

value = QVariant(mi.data(role)) ;
}
else
value = QSqlRelationalTableModel::data(index, role);

return value;
}

what am I missing?