[Qt-interest] [SOLVED] QTableView and QTreeView item background

Andrei Sebastian Cimpean andreiamenta at gmail.com
Sat Apr 17 19:22:51 CEST 2010


Hello Frank

It seems research does pay off and after a few hours of trials and errors I 
found the answer. I know this may be a known fact, but maybe some beginners 
like me will find the details helpful. Had to subclass QStyledItemDelegate and 
then set it as item delegate.
This is the code that works:

void ItemColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem 
&option, const QModelIndex &index) const {
    if(index.data().isValid()){
        QStyleOptionViewItem  viewOption(option);
        painter->save();
        painter->fillRect(option.rect,QColor(660033));
        painter->restore();
        viewOption.palette.setColor(QPalette::Text,QColor(660033));//<-text 
color obviously :P
        QStyledItemDelegate::paint(painter,viewOption,index);
    }
    else{
        QStyledItemDelegate::paint(painter,option,index);
    }
}

Hope it helps someone else, as it would have helped me.


On Friday 16 April 2010 18:47:25 K. Frank wrote:
> Hi Andrei -
> 
> On Fri, Apr 16, 2010 at 10:45 AM, Andrei Sebastian Cimpean
> 
> <andreiamenta at gmail.com> wrote:
> > Hello everybody.
> > 
> > I am playing around with a QTableView and QTreeView...
> > ...
> > Basically I want to change the background of some files and
> > folders(=items in the views), when they are displayed. My problem is the
> > actual background coloring.
> 
> (I don't know anything about background coloring.)
> 
> > Also I am curious if it is possible to add a widget or picture inside a
> > view item?
> 
> If I understand correctly what you are asking, I don't believe that
> there is any easy
> (i.e., with functionality already built in to Qt) way to do this.
> (Don't take my opinion
> too seriously, though.  A lot of stuff is possible in Qt that I don't
> know about.)
> 
> The question was asked before:
> 
>    http://lists.trolltech.com/pipermail/qt-interest/2010-March/020045.html
> 
> and no simple recipe was forthcoming.
> 
> (If you discover an answer, I'm sure Bertwim -- he started the archived
> thread, above -- would like to hear about it.  I would too.)
> 
> > Thank you
> 
> Good luck.
> 
> K. Frank
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100417/7d4ab619/attachment.bin 


More information about the Qt-interest-old mailing list