I would like to draw a line inside QTableWidgetItem
.
To draw the line I have reimplemented the QStyledItemDelegate::paint
method.
But, when I'm scrolling or selecting an item in the QTableWidget
.
Some of the items loose their drawing effect.
Here's my paint implementation:
void DrawLineDelegate::paint(QPainter *poPainter, const QStyleOptionViewItem &oOption, const QModelIndex &oIndex) const
{
// Valid index ?
if(!oIndex.isValid())
// Not valid.
return;
const QRect oRect( oOption.rect );
// Painter has certain settings
poPainter->save();
// Draw line
QColor oLineColor (oIndex.data(Qt::UserRole).toString());
poPainter->setRenderHint(QPainter::Antialiasing);
poPainter->setPen(QPen(oLineColor, 2, Qt::SolidLine, Qt::RoundCap));
poPainter->drawLine(oRect.left(), // Start X-coordinate
oRect.top() - oRect.height() / 2 , // Center Height (Y-coordinate)
oRect.left() + oRect.width(), // Line width
oRect.top() - oRect.height() / 2); // Center Height (Y-coordinate)
poPainter->restore();
QStyledItemDelegate::paint( poPainter, oOption, oIndex );
}
In the TableWidget init function I set the delegate item like this
ui->tableWidget->setItemDelegateForColumn(2, new DrawLineDelegate(this) );
Note : Each item I store color name as Qt::UserData
.
On table init the lines are drawing fine, the bug is when I'm playing with the table.
Here are some screenshots
Any suggestions ?