删除 QTableView 的垂直网格线 [英] Remove the vertical grid lines of a QTableView

查看:76
本文介绍了删除 QTableView 的垂直网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTableView 如下所示:

I have a QTableView as shown below:

我想从表格中删除所有垂直线.我尝试将 gridline-color 属性设置为与 background-color 等效,但它删除了所有网格线.

I want to remove all the vertical lines from the table. I tried to set the gridline-color property equivalent to the background-color, but it removed all the grid lines.

我希望保留水平网格线,并移除垂直网格线.我怎样才能做到这一点?

I want the horizontal grid lines to stay, and remove the vertical ones. How can I achieve that ?

推荐答案

delegate.h

delegate.h

class QLineDelegate : public QStyledItemDelegate
{
    public:
    QLineDelegate(QTableView* tableView);

    protected:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;

    private:
    QPen pen;
    QTableView* view;
};

委托.cpp

QLineDelegate::QLineDelegate(QTableView* tableView)
{
    int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());
    QColor gridColor = static_cast<QRgb>(gridHint);
    pen = QPen(gridColor, 0, tableView->gridStyle());
    view = tableView;
}

void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const
{
    QStyledItemDelegate::paint(painter, option, index);
    QPen oldPen = painter->pen();
    painter->setPen(pen);

    //draw verticalLine
    //painter->drawLine(option.rect.topRight(), option.rect.bottomRight());

    //draw horizontalLine
    //painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
    //above code, line have breakpoint, the following code can solve it well 

    QPoint p1 = QPoint(itemOption.rect.bottomLeft().x()-1,itemOption.rect.bottomLeft().y());
    QPoint p2 = QPoint(itemOption.rect.bottomRight().x()+1,itemOption.rect.bottomRight().y());
    painter->drawLine(p1, p2);
    painter->setPen(oldPen);
}

tableview.cpp

tableview.cpp

tableView->setShowGrid(false);
tableView->setItemDelegate(new QLineDelegate(tableView));

这篇关于删除 QTableView 的垂直网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆