Results 1 to 4 of 4

Thread: Paint in a QtableView

  1. #1
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Paint in a QtableView

    I'm trying to paint a alternating color in a QtableView (can't use the default alternating color, or a delegate because the delegate only paints for existing rows and not the entire table)

    im painting in the viewport this works fine until scrollbars are needed and start scrolling then the alternating colors do not linup with the rows

    how do i compensate for the scrolling?


    this is the code for painting in the viewport (this has alignment problems)
    Qt Code:
    1. void TableView::paintEvent(QPaintEvent *e)
    2. {
    3. int rowH = this->rowHeight(0);
    4. if(rowH == 0)
    5. rowH = 30;
    6.  
    7. QPainter painter(this->viewport());
    8.  
    9. int vpH = this->viewport()->height();
    10.  
    11. int count = vpH/rowH;
    12. QBrush oldBrush = painter.brush();
    13.  
    14. for(int i = 0; i <= count; i++)
    15. {
    16. //painter.drawLine(0,i*rowH-1,this->viewport()->width(),i*rowH-1);
    17. if(i%2 != 0)
    18. painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().alternateBase()); //odd color
    19. else
    20. painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().background()); //even color
    21. }
    22. painter.setBrush(oldBrush);
    23.  
    24. QTableView::paintEvent(e);
    25. }
    To copy to clipboard, switch view to plain text mode 

    i tried to paint the entire widget and let the scroll area take care of scrolling the widget but im getting the following messages:

    QWidget:aintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::brush: Painter not active
    QPainter::setBrush: Painter not active

    this is the code for painting in the widget (gives me the messages)
    Qt Code:
    1. void TableView::paintEvent(QPaintEvent *e)
    2. {
    3. int rowH = this->rowHeight(0);
    4. if(rowH == 0)
    5. rowH = 30;
    6.  
    7. QPainter painter(this);
    8.  
    9. int vpH = this->height();
    10.  
    11. int count = vpH/rowH;
    12. QBrush oldBrush = painter.brush();
    13.  
    14. for(int i = 0; i <= count; i++)
    15. {
    16. //painter.drawLine(0,i*rowH-1,this->viewport()->width(),i*rowH-1);
    17. if(i%2 != 0)
    18. painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().alternateBase()); //odd color
    19. else
    20. painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().background()); //even color
    21. }
    22. painter.setBrush(oldBrush);
    23.  
    24. QTableView::paintEvent(e);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by StrikeByte; 18th December 2014 at 11:30.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Paint in a QtableView

    can't use the default alternating color
    And why not? Are the colors wrong for your needs? Then change them by changing the palette for your QTableView instance. The painting code in the table widget should be doing something very similar to what you are trying.

  3. #3
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Paint in a QtableView

    Alternating colors only paints the filled rows and not the entire table, in the treeview there is a option that will paint entire view but that option is not available in the tableview

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Paint in a QtableView

    The easiest way to solve this might be to place a proxy model between your original model and the table view. The proxy will be dedicated to the view, so it can keep track of the number of filled vs. unfilled rows visible on the screen. You'll probably need to give it a pointer to the view so it can ask for geometry information; you might also connect it to scrollbar signals in the table view so it can recompute the number of "virtual rows" (model rows + empty visible rows).

    In this proxy, you should re-implement rowCount() to return the count of "virtual rows". You will also need to re-implement the data() method. For each row index < source model row count, you simply call the source model's data() method. For any row index >=source model row count, you need to return an empty QVariant for the DisplayRole (to show an empty cell), and a QBrush with either the QPalette::Base or QPalette::AlternatingBase color for the BackgroundRole depending on whether the virtual row index is odd or even.

    If recomputing the the virtual row count is too much hassle, you could simply say that the number of virtual rows is equal to the number of model rows plus one screen full of empty rows, and set the view's scrollbar limits appropriately. That way, unless the view is resized you can always be assured that the user can never scroll below the size of a screen full of empty rows.
    Last edited by d_stranz; 19th December 2014 at 16:46.

Similar Threads

  1. QTableView paint row in stylesheet hover
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2012, 09:17
  2. Replies: 1
    Last Post: 11th May 2011, 12:51
  3. Immediate paint
    By zgulser in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2010, 07:34
  4. Replies: 2
    Last Post: 26th November 2009, 04:45
  5. paint
    By xyzt in forum Qt Programming
    Replies: 3
    Last Post: 22nd March 2008, 18: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.