You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Jean__

Beginner

  • "Jean__" is male
  • "Jean__" started this thread

Posts: 34

Location: Groningen

  • Send private message

1

Thursday, January 10th 2008, 12:35pm

how to color a cell in a QTableView

How can I color a cell in a QTableView?
For instance if the cell holds a '1' I want it blue, else leave it white.

I know I have to override QPaintEvent, but how? I have a pointer to the QTableView that was created in Designer, it's called 'table'. :)

Now what to do?

table->paintEvent = somefuntion(QPaintEvent*)

and where should that function go?

I'm totally unclear about this and searches have not shown much in the way of a clear example.
Thanks.

This post has been edited 1 times, last edit by "Jean__" (Jan 10th 2008, 12:36pm)


flowerJT

Trainee

  • "flowerJT" is male

Posts: 73

Location: Berlin, Germany

  • Send private message

2

Thursday, January 10th 2008, 7:18pm

You can not do it the way you were suggesting it. Designer already made an instance of QTableView for you. But anyway, I don't think this is the right place at all. You have to subclass QAbstractItemDelegate in order to provide custom appearance - at least I think so ;)

Take a look at
QAbstractItemDelegate * QAbstractItemView::itemDelegate ( const QModelIndex & index ) const and
QAbstractItemDelegate.

Hope this helps.

PS: Sometimes you have to subclass a standard class as you mentioned and you want to use your class in designer. Than drag the base class into your form and right click on it. There's a menu entry called something like "Promote to custom widget...". Now you can enter class name, path, etc. Designer will create the ui-file accordingly.

Best regards

This post has been edited 2 times, last edit by "flowerJT" (Jan 10th 2008, 7:39pm)


Jean__

Beginner

  • "Jean__" is male
  • "Jean__" started this thread

Posts: 34

Location: Groningen

  • Send private message

3

Friday, January 11th 2008, 6:22pm

he thanks,

Well that class certainly looks interesting. Now if there were some examples as how to use it, that would be fine. Prblem with this is, a lot of people want to know how to do this but no-one seems to have much luck. Me I'm at the moment a bit weary of spitting through endless classes and members witout much of working examples.

The other technique you mentioned is interesting, gonna have a closer look.

Thanks.

Jean__

Beginner

  • "Jean__" is male
  • "Jean__" started this thread

Posts: 34

Location: Groningen

  • Send private message

4

Sunday, January 13th 2008, 12:01pm

Solved

Got it working
For those interested my code:

in the .h file add:

Source code

1
2
3
4
5
6
7
8
class Delegate : public QItemDelegate
 {
 Q_OBJECT
 public:
 Delegate(QWidget *parent = 0) : QItemDelegate(parent) {}
 void paint(QPainter *painter, const QStyleOptionViewItem &option,
                const QModelIndex &index) const;
 };


in the .cpp, in a constructor

Source code

1
table->setItemDelegate(new Delegate);


and the event:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//---------------------------------------------------
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
{
QString val;
if (qVariantCanConvert<QString>(index.data()))
    val = qVariantValue<QString>(index.data());
if (val == "1")
	{
  painter->fillRect(option.rect, option.palette.highlight());
	}
else
  QItemDelegate::paint(painter, option, index);
}