Hi,
I want to make two-colored cell in QTableView.
I made my delegate class with the following paint method:
void BitViewDelegate
::paint(QPainter *painter,
{
QString text
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
QRect myRect
= option.
rect;
myRect.setX(option.rect.x() + option.fontMetrics.width(l));
myRect.setWidth(option.rect.width() - option.fontMetrics.width(l));
painter->fillRect(myRect, Qt::green);
}
void BitViewDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::DisplayRole).toString();
QString l = text.left(5);
QRect myRect = option.rect;
myRect.setX(option.rect.x() + option.fontMetrics.width(l));
myRect.setWidth(option.rect.width() - option.fontMetrics.width(l));
painter->fillRect(myRect, Qt::green);
QItemDelegate::paint(painter, option, index);
}
To copy to clipboard, switch view to plain text mode
But there is a little problem. My rectangle and previous character (the one before the text.left(5) string) overlap.
It seems to me that there is a little margin between the border of the cell and default delegate.
How it can be calculated?
Or is there any other better solution how to make two-colored cells?
Bookmarks