[QTable] Change row's color
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
Re: [QTable] Change row's color
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-p...item-2713.html (look for SRadioButtonTableItem::paint).
Re: [QTable] Change row's color
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
Code:
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
Code:
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);
}
Re: [QTable] Change row's color
Try QColorGroup::Base instead of QColorGroup::Background.
Re: [QTable] Change row's color
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!
Re: [QTable] Change row's color
Quote:
Originally Posted by
villy
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.