[PyKDE] Colour a cell in QTableView (PyQt4)

Chris Dunscombe cdunscombe at yahoo.com
Fri Sep 22 11:11:51 BST 2006



--- Andreas Pakulat <apaku at gmx.de> wrote:

> On 21.09.06 10:29:39, Chris Dunscombe wrote:
> > --- Andreas Pakulat <apaku at gmx.de> wrote:
> > 
> > > On 21.09.06 03:27:04, Chris Dunscombe wrote:
> > > > I'm trying to set the colour of cell in a QTableView, not all cells the same colour. I'm
> using
> > > a
> > > > model with a delegate. I've looked at the docs and googled around and can't work out any
> > > > reasonable way to do it. In PyQt3 it was fairly easy as I just sub-classed QTable and
> > > > re-implemented paintCell.
> > > 
> > > It's easy with Qt4 too, in your model return the proper color if role ==
> > > Qt.BackgroundColorRole and the index is the one you want the color
> > > changed for. Of course if your custom delegate paints the cells, you
> > > need to ask the model for the background role data in the painting
> > > function. Look at the QItemDelegate's painting function to get an idea.
> > > 
> > 
> > Thanks for that pointer. However I'm still struggling. My custom delegate does paint the cell
> and
> > I've looked at QItemDelegate.paint(). I assume I need to re-implement this function, which
> I've
> > done, but I don't have any idea what I should actually code to set the colour. I tried
> creating my
> > own QBrush and setting it's colour etc.
> > 
> > def paint(self, painter, option, index)
> > 
> >     myBrush = QBrush()
> >     myBrush.setColor(Qt.Yellow)
> >     painter.setBrush(myBrush)
> 
> Look again at the paint method, line 273 and 274. All the delegate does
> (in case of not-selected cells) is filling the whole rectangle given via
> the option with the color.
> 
> For selected items the code is a bit harder to understand and I can't. I
> never dug that deep into Qt yet.
> 
> Andreas

Thanks I've finally got it sorted. Here's the code just in case someone else will benefit:

def paint(self, painter, option, index):

        r = option.rect
        painter.save() 
        mainColor = QColor(Qt.yellow)
        mainLight = mainColor.light(175)
        
        if self.myCellReadOnly(index.row(), index.column()):
            painter.fillRect(r, mainLight)
            painter.drawText(r, Qt.AlignVCenter,
                             " " + index.model().data(index, Qt.DisplayRole).toString())
        else:
            QItemDelegate.paint(self, painter, option, index)
        painter.restore()
 
Chris


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the PyQt mailing list