class MyTreeView : public QTreeView{public: MyTreeView(QWidget *parent = 0) : QTreeView(parent) { }protected: void paintEvent(QPaintEvent *event) { QTreeView::paintEvent(event); QPainter painter; painter.begin(viewport()); painter.save(); QPen pen(Qt::lightGray); pen.setStyle(Qt::DotLine); pen.setWidth(2); painter.setPen(pen); // рисуем вертикальную линию между столбцами QLine line(columnWidth(0), 0, columnWidth(0), height()); painter.drawLine(line); painter.restore(); painter.end(); } void drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const { QTreeView::drawRow(painter, options, index); painter->save(); QPen pen(Qt::lightGray); pen.setStyle(Qt::DotLine); pen.setWidth(2); painter->setPen(pen); // горизонтальная линия между полями QLine line(options.rect.left(), options.rect.bottom(), options.rect.right(), options.rect.bottom()); painter->drawLine(line); painter->restore(); }};
C++ (Qt)void QStyledItemDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const [virtual]
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QStyledItemDelegate::paint(painter, option, index); painter->save(); QPen pen(Qt::lightGray); pen.setStyle(Qt::DotLine); pen.setWidth(2); painter->setPen(pen); QRect rc(option.rect); if (index.column() == 0) rc.setLeft(0); QLine lines[2] = { QLine(rc.left(), rc.bottom(), rc.right(), rc.bottom()), // горизонтальная линия снизу QLine(rc.right(), rc.top(), rc.right(), rc.bottom()) // верт. линия справа }; painter->drawLines(&lines[0], index.column() == 0 ? 2 : 1); painter->restore();}
QRect rc(option.rect); if (index.column() == 0) rc.setLeft(0);
... MyTreeView *tree = dynamic_cast<MyTreeView*>(parent()); QRect rc(option.rect); if (index.column() == 0) rc.setLeft(-tree->m_hScrollPos); ...
C++ (Qt)MyTreeView *tree = dynamic_cast<MyTreeView*>(parent()); QRect rc(option.rect); if (index.column() == 0) rc.setLeft(-tree->horizontalScrollBar()->value());