Qt tableView - 添加颜色

时间:2014-03-27 15:16:32

标签: c++ qt qt5 qtgui

如何在tableView

的两侧添加颜色

enter image description here

1 个答案:

答案 0 :(得分:1)

你应该继承QHeaderView并实现你自己的类,如:

#include<QtWidgets>
class HeaderView: public QHeaderView
{
public:
  HeaderView():QHeaderView(Qt::Vertical)
  {}

   void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
   {
     QBrush brush;

     if(logicalIndex == 0)
        brush.setColor(QColor(Qt::red));
     else
        brush.setColor(QColor(Qt::blue));

     painter->fillRect(rect, brush); 
     QPen pen(Qt::white);
     painter->setPen(pen);     
     painter->drawText(rect,QString("Row %1").arg(logicalIndex));     
   }
};

接下来将HeaderView的实例设置为QTableView的垂直标题:

HeaderView vView;    
tableview.setVerticalHeader(&vView);