PDA

View Full Version : [QTable] Change row's color



villy
14th November 2006, 16:11
Hi all,
I want to change the color of a row in a table, when a checkbutton is checked. Checkbox is a QChecktableItem in a column. I've captured valueChanged(int, int) emitted by the QTable, but i don't know how to change the background color of the row :(
Can somebody help me?

Thnaks

jacek
14th November 2006, 19:13
You have to subclass QCheckTableItem and reimplement paint(). All you have to do inside it is to change background color in QColorGroup that was passed to paint() and invoke original paint() implementation.

Here's something similar: http://www.qtcentre.org/forum/f-qt-programming-2/t-qradiobuttontableitem-like-qchecktableitem-2713.html (look for SRadioButtonTableItem::paint).

villy
15th November 2006, 10:52
Thank you!
I've tried to do it but it doesn't work. Row color doesn't change :( :( What's wrong?!?

This is the declaration



class EnableTableItem : public QCheckTableItem {
public:
EnableTableItem(QTable * table, const QString & txt);
~EnableTableItem();

protected:
virtual void paint (QPainter *p, const QColorGroup &cg,
const QRect &cr, bool selected);
};


And this is my paint reimplementation



void EnableTableItem::paint( QPainter * p, const QColorGroup & cg,
const QRect & cr, bool selected )
{
QColorGroup color(cg);
if (isChecked())
color.setColor(QColorGroup::Background, Qt::green);
else
color.setColor(QColorGroup::Background, Qt::red);

QCheckTableItem::paint(p, color, cr, selected);
}

jacek
15th November 2006, 14:16
Try QColorGroup::Base instead of QColorGroup::Background.

villy
15th November 2006, 16:06
Oh yes :) That's right!
But now, if I want to change the color of the entire row, I think's it's better to subclass QTable and reimplement paint method... it's the right way?

Thanks a lot!

jacek
15th November 2006, 19:21
if I want to change the color of the entire row, I think's it's better to subclass QTable and reimplement paint method... it's the right way?
You can reimplement QTable::paintCell() exactly the same way and you'll get the whole row painted in green or red, but you can also stay with custom table items.

It just depends whether you want to have a custom table or custom items.