I might think of several ways to do what you need; both will include custom mesh drawing, since there seems to be no direct way to connect to the QTableView class's mesh drawing routine:
1. Switch the standard grid for your treeview grid by calling setShowGrid (false) and draw grid lines for the cells that need them using the element delegate. The following is an example:
class CustomDelegate : public QStyledItemDelegate
{
public:
CustomDelegate(QTableView* tableView);
protected:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
private:
QPen _gridPen;
};
CustomDelegate::CustomDelegate(QTableView* tableView)
{
int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());
QColor gridColor = static_cast<QRgb>(gridHint);
_gridPen = QPen(gridColor, 0, tableView->gridStyle());
}
void CustomDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter, option, index);
QPen oldPen = painter->pen();
painter->setPen(_gridPen);
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
if (index.column()!=1)
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->setPen(oldPen);
}
ui->tableView->setShowGrid(false);
ui->tableView->setItemDelegate(new CustomDelegate(ui->tableView));
2. Create a QTableView descendant and override the paintEvent method . There you can either draw your own grid, or let the base class draw it, and then draw horizontal lines on top of the grid using the background color of the table.
hope this helps, believes
source
share