PDA

View Full Version : A Question regarding QSortFilterProxyModel



Pluvius
10th January 2012, 11:06
Hello guys!

Whenever I retrieve a row from a QSqlTableModel the output on the view is naturally like this:
7249

I would like to "customize" the way data is displayed like in the following image:

7250

At first I had a wild idea of subclassing a QTableView but my guess is to manipulate the QModelIndexes and map them to new ones in the QSortFilterProxyModel but I'm not sure if this is the way to go, could you please help me on this one?

Thanks!

Lykurg
10th January 2012, 11:23
You could go for a QTreeView with a custom model (or use QSortFilterProxyModel) or provide a custom delegate, which fakes the view you like.

Pluvius
10th January 2012, 12:08
This is very helpful thanks! I'll try to come up with a solution based off your suggestions :)

Pluvius
17th January 2012, 12:11
Hello Again!

Just wanted to give a quick update; I chose to implement a custom model, subclassing naturally QAbstractTableModel, and since it's read-only, I've had just four methods; rowcount, colmuncount, headerData, but the problem is with Data() method, here's what I did:


QVariant CustomModel::data( const QModelIndex &index, int role ) const
{
QString name;
QSqlQuery q("select name from employee where id=3");
while (q.next()) {
QVariant resultat = q.value(0);
name = resultat.toString();
}

switch( role )
{

case Qt::DisplayRole:

if (index.column()==1 && index.row()==2)
return name;

default:

return QVariant();
}
}


This works as I can display the data right where I want it to, but I'm not sure if this is the right way to go, any opinions?

Thanks