PDA

View Full Version : PyQt Filling in cells with varying amounts of color



degan144
6th August 2015, 03:19
Using PyQt4 I'm trying to create an application that shows when a user spent time inside a "match". I want to indicate this in a way very similar to the way Google displays calendars (see image below). I cannot figure out how to make the cells in a tableview display a color with a size corresponding to the length of the match and how to put text layered on top of that.

11307

anda_skoa
6th August 2015, 11:24
Your model an simply return the color as Qt::BackgroundRole from its data() method.

The view will automatically use this to color the cell before writing values into it.

Cheers,
_

degan144
7th August 2015, 02:08
Thanks for the response. I've tried doing that however that (as far as I can tell) does not allow me to determine how much of the cell I color it instead fills the cell completely with the QColor. If I could return something like a QRect through the method you describe than I could maybe control how much of the cell (vertically) I fill. However, I have not found a way to do that. Alternatively I don't understand Qt that much but maybe use of an item delegate or a painter would work?

jefftee
7th August 2015, 04:10
My first thought for an application view like that would be a QGraphicsView, where you could draw the grid, rectangles of various shapes, etc. I think that would be less work IMHO that trying to force your will on a QTableView.

Did you consider that approach and rule it out?

anda_skoa
7th August 2015, 08:45
If I could return something like a QRect through the method you describe than I could maybe control how much of the cell (vertically) I fill.

Ah, I misunderstood, sorry.

If you only want to partially fill a cell you'll have to do this with a delegate.
It gets the information about cell geometry and has the full capability of the QPainter API at its disposal.

Cheers,
_