3

I Have QTableView object with horizontal headerView, (vertical I have hidden). I set setShowGrid(false) to remove grid from qtableView, but how can I remove separator borders between QTableView and its horizontal header. I tried:

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

but without success. thank you

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tokafr
  • 109
  • 2
  • 12

2 Answers2

4

If you mean the same "border" as I do, it's a part of the current style. So, if you want to get rid of it, you have to define your custom style using the style sheets.

Here is an example:

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

tableView->horizontalHeader()->setStyleSheet(style);

This style sheet sets an overall border of your header section to the 1px width black line and hides the bottom border.

Note: I am using a C++11 raw string literal here, so don't get confused. It's just a string.

Tomas
  • 2,170
  • 10
  • 14
  • Thank you for your answer, But there is one problem, I cannot use setStyleSheet() method, don't you know how to do the same with c++ commands, without css? – tokafr Mar 22 '16 at 08:56
  • A am afraid there is no method in the Qt API to switch the bottom border of the header off. If you can't use the style sheets there is another option: Reimplement the QHeaderView::paintSection() method. – Tomas Mar 22 '16 at 09:01
4

OK I reimplemented paintSection method and now I have what I wanted/

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());
}
tokafr
  • 109
  • 2
  • 12