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, a double int array is used which is of the same dimensions as the table. Depending on the values of 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 the 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:

Qt Code:
  1. void Sphere::paint(QPainter *painter, const QStyleOptionViewItem &option,
  2. const QModelIndex &index) const
  3. {
  4. if(tb1[index.row()][index.column()] == 1)
  5. {
  6. QImage Q1("Red Sphere.jpg");
  7.  
  8. QRectF source(0.0, 0.0, 72.0, 70.0);
  9.  
  10. painter->eraseRect(option.rect);
  11.  
  12. if (option.state & QStyle::State_Selected)
  13. painter->fillRect(option.rect, option.palette.highlight());
  14.  
  15. painter->drawImage(option.rect, Q1, source);
  16.  
  17. }
  18. else if(tb1[index.row()][index.column()] == 2)
  19. {
  20. QImage Q1("Blue Sphere.jpg");
  21.  
  22. QRectF source(0.0, 0.0, 72.0, 70.0);
  23.  
  24. painter->eraseRect(option.rect);
  25.  
  26. if (option.state & QStyle::State_Selected)
  27. painter->fillRect(option.rect, option.palette.highlight());
  28.  
  29. painter->drawImage(option.rect, Q1, source);
  30.  
  31. }
  32. else
  33. {
  34. QStandardItemModel *model = (QStandardItemModel*) index.model();
  35. if(!model->item(index.row(), index.column())->isEnabled())
  36. QStyledItemDelegate::paint(painter, option, index);
  37. else
  38. {
  39. painter->eraseRect(option.rect);
  40. painter->fillRect(option.rect, Qt::white);
  41. }
  42. }
  43. }
To copy to clipboard, switch view to plain text mode 

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