【问题标题】:Qt tableView - add colorQt tableView - 添加颜色
【发布时间】:2014-03-27 15:16:31
【问题描述】:

如何在tableView 的侧面添加颜色?

【问题讨论】:

  • 使用QStyledItemDelegate
  • @Dmitry Sazonov 如何获取 tableView 的属性? Qt :: DisplayRole?请用代码解释,我更容易理解。

标签: c++ qt qt5 qtgui


【解决方案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);

【讨论】:

  • 谢谢,现在可以了。必须将tableview.setHorizontalHeader (&amp;hView); 替换为tableview.setVerticalHeader(&amp;hView);
猜你喜欢
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2019-06-24
  • 1970-01-01
相关资源
最近更新 更多