开发者

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

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 value开发者_JAVA百科s 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:

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 
     {
         painter->eraseRect(option.rect);

         QStyledItemDelegate::paint(painter, option, index);
     }
  } 

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


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.

I think you have to determine witch cells are affected by the change made by the user in the current cell and force those cells to update(). When you think of it, your delegate can check if the content of the tb1 change to automatically decide to repaint the cell it belong to.

EDIT

A simple way to do that, could be to connect a slot to the clicked ( const QModelIndex & index ) then in here determine what change and call the update ( const QModelIndex & index ) method ...


According to QT Documentation

After painting, you should ensure that the painter is returned to its the state it was supplied in when this function was called. For example, it may be useful to call QPainter::save() before painting and QPainter::restore() afterwards.

I think you are missing save() and restore() methods of QPainter in your function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜