如何更改 QTreeView 标题(又名 QHeaderView)的背景颜色?

发布于 2024-12-03 18:30:06 字数 808 浏览 0 评论 0原文

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

HeaderView 不像 QTreeView 那样接受委托;它自己完成所有的绘画工作。它使用两种方法来完成此操作 -

我最初的尝试是尝试覆盖paintSection,让它绘制默认的东西,然后添加我自己的。

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

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

任何帮助表示赞赏。

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

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

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()

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).

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花似海 2024-12-10 18:30:07

无需实现任何QHeaderView,可以通过样式表就像几乎所有的小部件一样。

编辑:

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

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

您想要做出反应的角色是 Qt::BackgroundColorRole 所以函数看起来像这样

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()”,但如果正在进行大量计算,您可能希望缓存任何结果。

如果您使用的是 QStandardItemModel 您可能只需调用

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

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

Edit:

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]

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;
}

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.

If you are using the QStandardItemModel you can probably just call

setHeaderData(section, orientation, <aColor>, Qt::BackgroundColorRole);
甚是思念 2024-12-10 18:30:07

我认为你不需要覆盖任何东西。看看调色板 QWidget 的属性。

I think you don't need to override anything. Have a look at the palette property of QWidget.

醉生梦死 2024-12-10 18:30:07

我同意“Harald Scheirich”样式表是正确的选择。但也许为了说服你,我会告诉你样式表中一个不太为人所知的部分,称为属性选择器

如果你看一下样式表文档 此处,您会看到关于它们的一小部分。基本上我们正在做的是为那些具有特定属性集的小部件指定样式表。例如,所有 QPushButton 都是扁平的。

QPushButton[flat="true"]
{
   background-color: blue
}

虽然这很酷,但它本身并不能真正帮助您。更令人惊奇的是,您可以将自己的属性添加到 QObject。这些被称为动态属性。这些也可以在属性选择器中使用

所以我可以创建一个像这样的样式表 - 其中highlightHeader是我的虚构属性

QHeaderView[highlightHeader="true"] 
{ 
   background-color: red
}

现在,您可以将此样式表全局应用到每个QHeaderView。但由于没有人将highlightHeader 设置为true,所以我们在任何地方都看不到任何红色。因此,下一步是当我们决定要将特定的 QHeaderView 设为红色时,我们称之为:

myHeaderView->setProperty("highlightHeader", true);
myHeaderView->style()->unpolish(myHeaderView);
myHeaderView->style()->polish(myHeaderView);

在第一行中,我们设置属性以触发我们想要的状态。接下来的两行是为了确保 Qt 重新评估小部件的样式。否则你可能看不到变化。

基本上就是这样。我希望这有帮助。

I agree with "Harald Scheirich" style sheets are the way to go. But perhaps to convince you I will tell you about a lesser known part of stylesheets called Property Selectors

If you take a look at the stylesheet doc here, you'll see a small section about them. Basically what we are doing is specifying a stylesheet for those widgets with a particular property set. For instance, all QPushButtons that are flat.

QPushButton[flat="true"]
{
   background-color: blue
}

Now while that is pretty cool, on it's own it doesn't really help you. What is even more amazing is that you can add your own properties to a QObject. These are known as dynamic properties. And these can also be used in property selectors

So I could create a stylesheet like this - where highlightHeader is my made-up property

QHeaderView[highlightHeader="true"] 
{ 
   background-color: red
}

Now, you can apply this style sheet globally to every QHeaderView. But since no one is setting highlightHeader to true, we won't see any red anywhere. So the next step is when we decide that we want to make a particular QHeaderView red, then we call this:

myHeaderView->setProperty("highlightHeader", true);
myHeaderView->style()->unpolish(myHeaderView);
myHeaderView->style()->polish(myHeaderView);

In the first line we are setting the property to trigger the state that we want. The next two lines are to ensure that Qt re-evaluates the style of the widget. Otherwise you probably won't see the change.

And basically that's it. I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文