PDA

View Full Version : Column Colour



sumsin
15th June 2006, 08:30
Hi,

How can I show a readonly column in Gray colour in a QTable.

wysota
15th June 2006, 08:46
You have to subclass QTableItem and reimplement its paint() routine. And of course use this subclass instead of QTableItem if you need such behaviour.

sumsin
15th June 2006, 10:48
Thanks



void MyTableItem::paint (QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected)
{
QColorGroup g(cg);
g.setColor( QColorGroup::Background, Qt::red );
QTableItem::paint (p, cg, cr, selected);
}



but its not working

wysota
15th June 2006, 11:06
How did you use this code?

sumsin
15th June 2006, 11:16
should I use something else???

wysota
15th June 2006, 11:22
Use "Base" instead of "Background".

BTW. There is an error in your code. You are calling paint() with cg instead of g.

sumsin
15th June 2006, 11:25
first i use Base but its also not working, then I use Background which is also not working.

Is there any other way to do so???

wysota
15th June 2006, 11:34
Did you correct that paint() call?

You have:

QTableItem::paint (p, cg, cr, selected);
and it should be:

QTableItem::paint (p, g, cr, selected);

This works for me:


#include <qapplication.h>
#include <qtable.h>

class MyTableItem : public QTableItem {
public:
MyTableItem(QTable *table) : QTableItem(table, QTableItem::OnTyping){}
void paint(QPainter *p, const QColorGroup &cg, const QRect &cr, bool selected){
QColorGroup g(cg);
g.setColor(QColorGroup::Base, Qt::red);
QTableItem::paint(p, g, cr, selected);
}

};

int main(int argc, char **argv){
QApplication app(argc, argv);
QTable tab(1,1);
tab.setItem(0,0, new MyTableItem(&tab));
app.setMainWidget(&tab);
tab.show();
return app.exec();
}

sumsin
15th June 2006, 11:38
Problem solved.
You are right. I should use g instead of cg.
Thanks.