Results 1 to 7 of 7

Thread: TableView and setting background color of a cell that was not modified

  1. #1
    Join Date
    May 2011
    Posts
    20
    Thanked 3 Times in 3 Posts
    Platforms
    Unix/X11

    Default TableView and setting background color of a cell that was not modified

    I have a custom table view, with custom model, and custom cell delegate.
    I need the ability to change the background color of a cell depending on the contents of another cell.
    For example if I modify cell (2,2) I may need to change the background color of cell(5,3).

    I originally set it up so it would set the background during the cell delegate's paint routine. the problem is when I modify cell (2,2) the paint routine is never getting called for cell (5,3). I can call repaint() on the entire table but I would prefer not to do this.

    I run into a similar issue if I try to set the background using the model. Another issue with the model is that the background role issued for the model is done after the cell delegate's paint routine which prevents me from using the cell delegate's paint routine to draw other things within the cell.

    Is there an easy way to do this? If so how should I proceed?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TableView and setting background color of a cell that was not modified

    Use Qt::BackgroundRole of the model to set the color.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    20
    Thanked 3 Times in 3 Posts
    Platforms
    Unix/X11

    Default Re: TableView and setting background color of a cell that was not modified

    Use Qt::BackgroundRole of the model to set the color.
    That won't solve my issue as now I can not style the cell since the Model paints the background color after the cell delegate paint event.
    For example I may want a wavy line to appear under the text in the cell.
    So what happens is the cell delegate paints the wavy line and then the model paints the background which cover's the wavy line.

    If there is a way to paint the wavy line using the model and not use the cell delegate?

  4. #4
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: TableView and setting background color of a cell that was not modified

    If some cell data has been changed and you need another cell's contents to be updated in turn you should do that via the model, something like this:
    Qt Code:
    1. bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3. int row = index.row();
    4. int col = index.column();
    5.  
    6. if(role == Qt::EditRole && col == SOME_SPECIAL_COLUMN)
    7. {
    8. // do editing
    9.  
    10. // update entire row
    11. QModelIndex left = index(row, 0);
    12. QModelIndex right = index(row, columnCount() - 1);
    13.  
    14. emit dataChanged(left, right);
    15.  
    16. return true;
    17. }
    18.  
    19. return false;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Inside your custom delegate's paint method you can paint the background like this
    Qt Code:
    1. void CustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. // figure out how the cell should be drawn
    5. // ...
    6.  
    7. QColor background = index.model()->data(index, Qt::BackgroundRole).value<QColor>();
    8. painter->fillRect(option.rect, background);
    9. }
    To copy to clipboard, switch view to plain text mode 

    A delegate is usually responsible for drawing a cell's contents. So if you provide a custom delegate there should be no class "overpainting" that cell or its background. If I still miss your point you need to provide some code.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TableView and setting background color of a cell that was not modified

    Quote Originally Posted by snydesc View Post
    That won't solve my issue as now I can not style the cell since the Model paints the background color after the cell delegate paint event.
    The model doesn't paint anything. Only the delegate does. You have complete control over what the delegate does.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    May 2011
    Posts
    20
    Thanked 3 Times in 3 Posts
    Platforms
    Unix/X11

    Default Re: TableView and setting background color of a cell that was not modified

    Ok I got it now.
    I did not realize that the model invokes the delegate's paint event.
    I also had my delegate do my custom styling and then I called the base QStyledItemDelegate:aint() which was just painting over the stuff I just painted.
    All is working, Thanks for your help.

  7. #7
    Join Date
    May 2011
    Posts
    20
    Thanked 3 Times in 3 Posts
    Platforms
    Unix/X11

    Default Re: TableView and setting background color of a cell that was not modified

    I am still having some issues. I need to change the color of some cells when I change a row. I call model->data(index, Qt::BackgroundColorRole); on an index located in the previous row but paint is not being called. Is there someway I can invoke the paint() for that cell? I do not want to emit datachanged() because the data did not change only the background color.

    Note that the cells changes to the correct color if I cover the widget up with another window and uncover it as this will invoke the paint routine.


    Added after 33 minutes:


    yay I think I found a solution. I ended up calling tableview->update() and passing it an index from the proxy model.
    Last edited by snydesc; 27th September 2012 at 23:44.

Similar Threads

  1. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  2. Replies: 1
    Last Post: 21st April 2011, 09:14
  3. color specified cell in a tableview
    By poporacer in forum Newbie
    Replies: 1
    Last Post: 29th March 2011, 20:52
  4. Changing the background color of a cell in a QTableView
    By scarleton in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2010, 13:23
  5. Replies: 1
    Last Post: 23rd March 2009, 21:36

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.