[Qt-interest] QTableView and QTreeView item background

David Boosalis david.boosalis at gmail.com
Tue Apr 20 06:15:45 CEST 2010


On Mon, Apr 19, 2010 at 3:22 PM, Carsten Breuer <CarstenBreuerQt at textwork.de
> wrote:

> Hi Andrei,
>
> > I am playing around with a QTableView and QTreeView, each using a
> > QFileSystemModel as model. Working with views is a bit different than
> what
> > appears to be a straight road with QTableWidget, at least when it comes
> to
> > coloring the background of just some items displayed.
>
> I see tho possibilities here:
>
> 1.) Write an Delegate and change the color there.
>
ItemDelegate does add a little complexity, but I think it pays off in the
long term.  The View container classes are just more flexible  then the
Widget container classes.    The hardest part about using a Delegate is
getting the data out to decide what to do or how to paint.  You can either
create your own Items that inherit from QStandardItem and get the data
though your own personal extensions of QStandardItem, or do what I did and
use QStandardItem::setData().  Then use a getData() in the Delegate:;paint()
method

For instance in my delegate class I have the paint methods as follows:
void  StockModelDelegate::paint(QPainter *painter,
              const QStyleOptionViewItem &option,
              const QModelIndex &index) const
{

  QStyledItemDelegate::paint(painter,option,index);

  QVariant v   = index.data(Qt::UserRole+2);
  QWidget *w   = (QWidget *) v.value<void *>();

  if (w->isVisible()) {
    QRect rect = option.rect;
    int x1,x2,y1,y2;
    x1 = option.rect.x();
    x2 = x1 + option.rect.width() - visiblePixmap->width()-4;
    y1 = option.rect.y();
    y2 = y1 + (option.rect.height()- visiblePixmap->width())/2;
    painter->drawPixmap(x2,y2,*visiblePixmap);
  }
}

This does not set the background color by I am sure you get the idea here.

Regards
David


> 2.) Not sure: Use a proxy model.
>
> > Also I am curious if it is possible to add a widget or picture inside a
> view
> > item?
>
> Check out the model help.
> I'm not user with this one.
> Check also Qt::DecorativeRole (or something like that.
>
>
> Regards,
>
>
>
> Carsten
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100419/c4df0f45/attachment.html 


More information about the Qt-interest-old mailing list