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)
{
int rowH = this->rowHeight(0);
if(rowH == 0)
rowH = 30;
int vpH = this->viewport()->height();
int count = vpH/rowH;
QBrush oldBrush
= painter.
brush();
for(int i = 0; i <= count; i++)
{
//painter.drawLine(0,i*rowH-1,this->viewport()->width(),i*rowH-1);
if(i%2 != 0)
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().alternateBase()); //odd color
else
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().background()); //even color
}
painter.setBrush(oldBrush);
}
void TableView::paintEvent(QPaintEvent *e)
{
int rowH = this->rowHeight(0);
if(rowH == 0)
rowH = 30;
QPainter painter(this->viewport());
int vpH = this->viewport()->height();
int count = vpH/rowH;
QBrush oldBrush = painter.brush();
for(int i = 0; i <= count; i++)
{
//painter.drawLine(0,i*rowH-1,this->viewport()->width(),i*rowH-1);
if(i%2 != 0)
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().alternateBase()); //odd color
else
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().background()); //even color
}
painter.setBrush(oldBrush);
QTableView::paintEvent(e);
}
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)
{
int rowH = this->rowHeight(0);
if(rowH == 0)
rowH = 30;
int vpH = this->height();
int count = vpH/rowH;
QBrush oldBrush
= painter.
brush();
for(int i = 0; i <= count; i++)
{
//painter.drawLine(0,i*rowH-1,this->viewport()->width(),i*rowH-1);
if(i%2 != 0)
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().alternateBase()); //odd color
else
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().background()); //even color
}
painter.setBrush(oldBrush);
}
void TableView::paintEvent(QPaintEvent *e)
{
int rowH = this->rowHeight(0);
if(rowH == 0)
rowH = 30;
QPainter painter(this);
int vpH = this->height();
int count = vpH/rowH;
QBrush oldBrush = painter.brush();
for(int i = 0; i <= count; i++)
{
//painter.drawLine(0,i*rowH-1,this->viewport()->width(),i*rowH-1);
if(i%2 != 0)
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().alternateBase()); //odd color
else
painter.fillRect(0,i*rowH,this->viewport()->width(),rowH,palette().background()); //even color
}
painter.setBrush(oldBrush);
QTableView::paintEvent(e);
}
To copy to clipboard, switch view to plain text mode
Bookmarks