[PyKDE] Colour a cell in QTableView (PyQt4)

Andreas Pakulat apaku at gmx.de
Thu Sep 21 20:17:48 BST 2006


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

-- 
You have had a long-term stimulation relative to business.




More information about the PyQt mailing list