【问题标题】:How to remove horizontal borders from QHeaderView如何从 QHeaderView 中删除水平边框
【发布时间】:2016-07-08 23:50:09
【问题描述】:

我有带有水平 headerView 的 QTableView 对象,(我隐藏了垂直的)。 我将 setShowGrid(false) 设置为从 qtableView 中删除网格,但是如何删除 QTableView 与其水平标题之间的分隔符边框。 我试过了:

tableView->horizontalHeader()->setFrameShape(QFrame::VLine)

但没有成功。 谢谢

【问题讨论】:

    标签: qt qtableview qheaderview qframe qabstractitemview


    【解决方案1】:

    好的,我重新实现了paintSection 方法,现在我得到了我想要的/

    void MyHeaderView::paintSection(QPainter *painter, const QRect &rect,  int logicalIndex) const
    {
      QString data = model() -> headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
    
      QFontMetrics fm = painter -> fontMetrics();
    
      painter -> fillRect(rect, QBrush(QColor("white")));
      painter -> drawText(rect, Qt::AlignLeft, data);
    
      painter -> drawLine(rect.topRight(), rect.bottomRight());
    }
    

    【讨论】:

      【解决方案2】:

      如果你的意思和我一样的“边框”,它是当前样式的一部分。所以,如果你想摆脱它,你必须使用样式表来定义你的自定义样式。

      这是一个例子:

      QString style = R"( QHeaderView::section {
                              border: 1px solid black;
                              border-bottom: 0px;             
                          }
                        )";
      
      tableView->horizontalHeader()->setStyleSheet(style);
      

      此样式表将标题部分的整体边框设置为 1px 宽的黑线并隐藏底部边框。

      注意:我在这里使用 C++11 原始字符串文字,所以不要混淆。这只是一个字符串。

      【讨论】:

      • 谢谢你的回答,但是有一个问题,我不能使用 setStyleSheet() 方法,你不知道如何在没有 css 的情况下使用 c++ 命令来做同样的事情吗?
      • 恐怕Qt API中没有方法可以关闭标题的底部边框。如果您不能使用样式表,还有另一种选择:重新实现 QHeaderView::paintSection() 方法。
      猜你喜欢
      • 2018-04-29
      • 2019-02-14
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多