如何更改QTreeView标头(又名QHeaderView)的背景色? [英] How to change the background color for a QTreeView Header (aka QHeaderView)?

查看:963
本文介绍了如何更改QTreeView标头(又名QHeaderView)的背景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改某些标题部分的背景颜色.有些将使用默认的颜色,另一些将使用不同的颜色.

I am trying to change the background color for certain header sections. Some will use the default coloring, others will get a different color.

HeaderView不像QTreeView那样接受委托.它完成所有绘画本身.它使用两种方法来实现-

The HeaderView doesn't accept delegates like the QTreeView does; it does all the painting itself. It does this using two methods --

  • paintEvent
  • paintSection

我最初的尝试是尝试覆盖paintSection,让其绘制默认材质,然后添加我自己的材质.

My initial attempt was to try and override paintSection, letting it paint the default stuff, and then adding my own.

def paintSection(self, painter, rect, logicalindex):
    QHeaderView.paintSection(self, painter, rect, logicalindex)
    painter.save()
    painter.fillRect(rect, QBrush(Qt.red))
    painter.restore()

这似乎没有任何作用.它不会绘制填充的矩形.如果我将对基础paintSection方法的调用注释掉,它将绘制填充的rect,但不是很一致(即单击并调整标题的大小有时会使其填充,而不是其他).

This doesn't appear to do anything. It will not draw the filled rect. If I comment out the call to the base paintSection method, it will draw the filled rect, but not very consistently (i.e. clicking and resizing the header causes it to fill sometimes and not others).

感谢您的帮助.

推荐答案

无需执行任何操作QHeaderView可以通过

There is no need to implement anythingQHeaderView can be changed through stylesheets like almost all widgets.

您提到要根据数据更改每列的背景颜色,最简单的方法可能是从QAbstractItemModel或其他模型类中派生新模型并重新实现headerData()调用

You mentioned that you wanted to change the background color per column depending on data, the easiest way to do that is probably to derive a new model from QAbstractItemModel or another model class and reimplement the headerData() call

QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const [virtual]

您要响应的角色是Qt::BackgroundColorRole,因此该功能看起来像这样

the role that you want to react to is Qt::BackgroundColorRole so the function could look like this

QVariant VariableHeaderModel::headerData(int section Qt::Orientation orientation, int role)
{
  QVariant result;
  if (role == Qt::BackgroundColorRole)
  {
    result = <QColor from custom processing>
  }
  else
  {
    result = Superclass::headerData(section, orientation, role);
  }
  return result;
}

通常在Qt中,模型决定要显示的内容,几乎所有时间都在更改模型,而不是视图.同样,"data()"调用也被调用很多,我不知道"headerData()",但是如果正在进行大量计算,您可能希望缓存任何结果.

Generally in Qt, the model decides what to show, almost all of the times change the model, not the view. Also the 'data()' calls get called a lot, I don't know about the 'headerData()' but you probably want to cache any results if there is a lot of calculation going on.

如果您使用的是QStandardItemModel,则可能只需致电

If you are using the QStandardItemModel you can probably just call

setHeaderData(section, orientation, <aColor>, Qt::BackgroundColorRole);

这篇关于如何更改QTreeView标头(又名QHeaderView)的背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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