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

franki franki at franki.eu.org
Thu Dec 17 23:36:56 CET 2009


Thursday 17 of December 2009 21:55:50 Magyar Bence napisał(a):
> Have you tried using Delegate?
> I never needed to color rows of a tableview, but you should try delegating.
> Create a custom delegate class and reimplement the paint() method.
> Something like this:
>
> void RegDelegate::paint(QPainter *painter, const QStyleOptionViewItem
> &option,const QModelIndex &index) const
> {
>        if(index.column() == coloredIfEqOneColumn) {
>             // if you need to color all cells then
>             // you have to set all of them one-by-one
>             // using index.child()
>        } else {
>        }
> }
>
> It's just a theory so it !might! work, not guaranteed :)

Thanks for answer, but I think my problem is something else, I know how to 
change color of the cell, or entire row. For example changing color for row 
number 1:
in function myModel::data there should be:

if(role == Qt::BackgroundColorRole && index.row()==1)
                return qVariantFromValue(QColor(Qt::blue));

and entire row is blue.

but my problem is different: I want to change something in the cell x,y (1,0) 
depending on the cell x,y (1,2). So I think there are two possibilities:
1. Somehow I change data inside model, and when QTableView queries model it 
takes proper values for role Qt::BackgroundColorRole for each cell I wanted
2. Inside function myModel::data when QTableView queries about cell (1,0) I 
have to know somehow what value is in cell (1,2), for example I walked 
throught model earlier, and I wrote down in separate array what was value for 
column number 2 in every row.

Surely some one had to do this earlier... because it simply example, there are 
many other roles that can be used by QTableView, and there must be a way to 
assign them values... but how ;)

Best Regards
Marek

>
> -Bence Magyar
>
> franki írta:
> > 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
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at trolltech.com
> > http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list