PDA

View Full Version : Adding more than one virtual column in proxy model



qavaxb
18th January 2013, 00:24
Hi,
I'm using QSqlTableModel to interact with sqlite database. I have connected QSortFilterProxyModel to filter results in QTableView.
Now I'm wondering how can I add two and more virtual columns that I don't have in model, but I want to have in view.
I have read the topic: http://www.qtcentre.org/threads/8070-QAbstractProxyModel-and-quot-virtual-quot-columns and http://www.qtcentre.org/threads/15654-QAbstractProxyModel-adding-a-virtual-column but it adds only one of column.
Which and how should I reimplement QSortFilterProxyModel to add more than one column? More precise, how is realized passing information about position in QTableView to model?

Thanks in advance for every hint.

wysota
18th January 2013, 01:33
Subclass QIdentityProxyModel, reimplement columnCount() to return the number of columns in the original model plus the number of artificial columns you want to have. Then reimplement data() and setData() to store and return data for your artificial columns.

qavaxb
21st January 2013, 19:29
I forgot to mention that I have also QWidgetMapper connected to add and edit comments, but I'm not sure if it is possible to pass data to model with QIdentityProxyModel.

Second thing is that I don't know how can I get infromation about number of column to insert data, for instance I've got three columns in model: value a, value b and comment. I want to add two columns: a+b and a*b. Problem is that I get only indexes from model and the rest of them are invalid, so it's not possible for me to distinguish column.


QVariant proxyCalcColumn::data(const QModelIndex &index, int role) const
{

QString tempString("Yo");
QVariant dataFromCell(tempString);

if (index.column()>=0) { // Here I get invalid number of column
return QSortFilterProxyModel::data(index, role);
} else {
return dataFromCell;
}

}

wysota
21st January 2013, 19:49
The column information is carried by the index. If you get invalid column number then apparently you're trying to access the model in an invalid way.

qavaxb
26th January 2013, 14:26
But the thing is that I don't want to modify my model (data from SQL database), rather I want to add two virtual columns that are not in the model, but they are visible through QTableView and created (calculated) in a proxy.

wysota
26th January 2013, 17:09
But the thing is that I don't want to modify my model (data from SQL database), rather I want to add two virtual columns that are not in the model
That's why you're implementing a proxy.