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.

1

Wednesday, June 8th 2011, 6:29pm

Some cells in QtableView are not repainted automatically after a clicking on a table cell

Hello,

I'm using a QTableView in the implementation of an interactive board game. Images are to be displayed in the cells of the table. I'm using a QStyledItemDelegate with a paint function to draw the images inside the table cells.
As the images should be displayed only in certain cells of the table and updated when a user clicks on a table cell, an double int array is used which is of the same dimensions as the table. Depending on the values the the array, the painter should draw images in specific cells of the table. Initially there are only 4 images inside 4 cells of the table and as the user clicks on a cell in the table, the array is updated which should consequently mean that whats drawn and displayed inside the cells of rthe table should be changed.

Normally the user clicks on an empty (white) cell which is updated successfully and the specific image is shown in the cell. However, if there are other cells which contain an image and should be updated, the update is not shown, although the double int array is updated. I also saw a weird thing, that is when I click on the cells in which their display should have been updated, the update happens. This of-course occurs regardless of how I update when someone clicks on a cell.

I tried to first erase whats inside the cell before redrawing, but its still not working. Does the delegate runs continuously in a thread and the painter function is called with the index of each cell in the table? I do not get how an update on a cell containing an Image does not update automatically although the painter should have redrawn the cell's area and it occurs only after the a click on the cell has been made. Or its cus a new painter is called to the painter's function each time?!
Well, here is my implementation of the painter's function of the delegate:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 void Sphere::paint(QPainter *painter, const QStyleOptionViewItem &option,
                      	const QModelIndex &index) const
 {
 	if(tb1[index.row()][index.column()] == 1)
	 {
		 QImage Q1("Red Sphere.jpg");

	 QRectF source(0.0, 0.0, 72.0, 70.0);

	 painter->eraseRect(option.rect);

	 if (option.state & QStyle::State_Selected)
         	painter->fillRect(option.rect, option.palette.highlight());

	 painter->drawImage(option.rect, Q1, source);

	 }
	 else if(tb1[index.row()][index.column()] == 2)
	 {
		 QImage Q1("Blue Sphere.jpg");

	 QRectF source(0.0, 0.0, 72.0, 70.0);

	 painter->eraseRect(option.rect);

	 if (option.state & QStyle::State_Selected)
         	painter->fillRect(option.rect, option.palette.highlight());

	 painter->drawImage(option.rect, Q1, source);

	 }
	 else 
	 {
		 QStandardItemModel *model = (QStandardItemModel*) index.model();
		 if(!model->item(index.row(), index.column())->isEnabled())
			 QStyledItemDelegate::paint(painter, option, index);
		 else
		 {
		 painter->eraseRect(option.rect);
		 painter->fillRect(option.rect, Qt::white);
		 }
	 }
 } 

I can give you any more info if u needed to solve my problem. Thanks in advance.

2

Friday, June 10th 2011, 8:47pm

I got it. I called update(const QModelIndex & index) in the painter's function of the delegate at the beginning (first line) and it worked. Not sure if this was the best way to do it but it worked. Thank you for trying to help I'm sure no one of succeed in finding the answer else I wold have seen replys or not?!