[Qt-interest] custom model question, cells depending on other cell

franki franki at franki.eu.org
Thu Dec 17 21:09:25 CET 2009


Hi,

I cant figure out how to change background color of the entire row in 
QTableView depending on the value of the one of the cells, for example: 
select a,b,c,d from my_table;
column "d" is never shown in QTableView, but if it has value=1 background 
color for this row should be, lets say blue.
According to documentation, I should made custom model, so this is what I did:
Constructor:

custSearchModel::custSearchModel(QObject *parent): QSqlQueryModel(parent)
{
}

data function:
QVariant custSearchModel::data(const QModelIndex &index, int role) const
{
        bool ok=false;
        QVariant value = QSqlQueryModel::data(index, role);
	if(index.column()==0 && index.row()==0) {
                qDebug() << "object role:"+QVariant(role).toString()+" object 
value:"+value.toString();
        }
}

As I noticed view asks several times about each object, fetching different 
roles, so it's not a problem to return what I want for that object. But when 
view fetches cell "a" how should I know what to return if that depends on 
cell "d" ?
Should I have some other array in memory to remember all row numbers where 
cell "d" has value=1 ? 
I thought also, that if I have custom model, I will be able to walk throught 
the entire model and by using setData, i will set what i want for each role, 
but now when I use setData, it always returns FALSE. 
For example (from other function outside custSearchModel class)

searchCustModel=new custSearchModel(this);
// some code to set model query, view, etc.
QModelIndex index=searchCustModel->index(0,0,QModelIndex());
if(searchCustModel->setData(index,qVariantFromValue(QColor(Qt::blue)), 
Qt::BackgroundColorRole))
	qDebug() << "setData ok";
else
	qDebug() << "setData failed";

Please help, how it should be done?

Thanks
Marek



More information about the Qt-interest-old mailing list