[Qt-interest] custom model question, cells depending on other cell
Magyar Bence
mw.mzperx at gmail.com
Thu Dec 17 21:55:50 CET 2009
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 :)
-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
>
More information about the Qt-interest-old
mailing list