Results 1 to 4 of 4

Thread: Paint in a QtableView

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.