Results 1 to 7 of 7

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

  1. #1
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Some cells in QtableView are not repainted automatically after a clicking on a 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, 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.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Some cells in QtableView are not repainted automatically after a clicking on a ce

    Quote Originally Posted by Mezzo
    I tried to first erase whats inside the cell before redrawing,
    erasing is not required, you only need to draw, rest is taken car by the framework

    update all the required items from the same scope were you update your double int array. Where do you update your double int array, in the view's event clicked()? If yes then call
    Qt Code:
    1. void QAbstractItemView::update(const QModelIndex & index)
    To copy to clipboard, switch view to plain text mode 
    in there for all the items that have to be updated.

  3. #3
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Some cells in QtableView are not repainted automatically after a clicking on a ce

    I called update ( const QModelIndex & index ) for each cell that should be updated but its still not working. I may have seen it work only once though.
    Last edited by Mezzo; 9th June 2011 at 15:38.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Some cells in QtableView are not repainted automatically after a clicking on a ce

    post how you call update(), and how create index ?

  5. #5
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Some cells in QtableView are not repainted automatically after a clicking on a ce

    Well, first the click signal is received in AtaxxClass as in connect(tableView, SIGNAL(clicked(QModelIndex)), AtaxxClass, SLOT(update()));

    then form the update function i use:

    QPoint Q(row, col);
    ui.tableView->update(ui.tableView->indexAt(Q));

    for each cell that should be updated, after updating the array.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Some cells in QtableView are not repainted automatically after a clicking on a ce

    Quote Originally Posted by Mezzo View Post
    Well, first the click signal is received in AtaxxClass as in connect(tableView, SIGNAL(clicked(QModelIndex)), AtaxxClass, SLOT(update()));
    There might be problems with this.

    1. AtaxxClass is subclass of QTableView, and QTableView already has a public slot named update(QModelIndex);
    2. Make sure the SIGNAL & SLOT functions signatures match, else the slot may will not be called

    then form the update function i use:
    Do you mean you implemented update function? You should not be doing so. You should be calling already exisiting QTableView::update(QModelIndex);

  7. #7
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4 Qt Jambi
    Platforms
    Windows

    Default Re: Some cells in QtableView are not repainted automatically after a clicking on a ce

    Well, first of all AtaxxClass is a subclass of QMainWindow. And yes, I've implemented the update function, this is were I update the array based on the user selection.


    Added after 1 28 minutes:


    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. Thanks for your help.
    Last edited by Mezzo; 10th June 2011 at 20:44.

Similar Threads

  1. Replies: 6
    Last Post: 16th February 2010, 18:21
  2. Replies: 1
    Last Post: 29th January 2010, 11:17
  3. Replies: 2
    Last Post: 11th March 2009, 02:00
  4. Replies: 3
    Last Post: 14th September 2007, 12:35
  5. Double clicking with QTableView
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2006, 06:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.