更改QTreeView的行背景颜色不起作用 [英] Changing the row background color of a QTreeView does not work

查看:3447
本文介绍了更改QTreeView的行背景颜色不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QTreeView ,并希望行的不同背景颜色,具体取决于它们的内容。为了实现这一点,我从 QTreeView 中导出一个类MyTreeView ,并实现了paint方法如下:

  void MyTreeView :: drawRow(QPainter * painter,
const QStyleOptionViewItem& option,
const QModelIndex& index)const
{
QStyleOptionViewItem newOption(option);

if(someCondition)
{
newOption.palette.setColor(QPalette :: Base,QColor(255,0,0));
newOption.palette.setColor(QPalette :: AlternateBase,QColor(200,0,0));
}
else
{
newOption.palette.setColor(QPalette :: Base,QColor(0,0,255));
newOption.palette.setColor(QPalette :: AlternateBase,QColor(0,0,200));
}

QTreeView :: drawRow(painter,newOption,index);
}



最初,我设置 setAlternatingRowColors / c>

我的问题:设置 QPalette :: Base的颜色没有效果

/ strong>。



但是,设置 QPalette :: AlternateBase会按预期工作
我尝试了 setAutoFillBackground(true) setAutoFillBackground(false) b
$ b

有没有提示如何解决这个问题?

通过调整 MyModel设置颜色。 :: data(const QModelIndex& int role) Qt :: BackgroundRole 不提供所需的结果。在这种情况下,背景颜色仅用于行的一部分。



Qt版本: 4.7.3






更新:
由于未知原因 QPalette :: Base 似乎是不透明的。
setBrush不会改变。
我发现了以下解决方法:

  if(someCondition)
{
painter- ; fillRect(option.rect,Qt :: red);
newOption.palette.setBrush(QPalette :: AlternateBase,Qt :: green);
}
else
{
painter-> fillRect(option.rect,Qt :: orange);
newOption.palette.setBrush(QPalette :: AlternateBase,Qt:blue);
}


解决方案

扩展/折叠控件没有背景像行的休息,然后在 :: data()中使用 Qt :: BackgroundRole $ c>(如 pnezis 他们的答案),并将其添加到您的树视图类:

  void MyTreeView :: drawBranches(QPainter * painter,
const QRect& rect,
const QModelIndex& index)const
{
if(某些条件取决于索引)
painter-> fillRect(rect,Qt :: red);
else
painter-> fillRect(rect,Qt :: green);

QTreeView :: drawBranches(painter,rect,index);
}



我使用Qt 4.8在Windows(Vista和7) 0和展开/折叠箭头具有适当的背景。问题是这些箭头是视图的一部分,因此不能在模型中处理。


I have a QTreeView and want different background colors for rows, depending on their content. To achieve this, I derived a class MyTreeView from QTreeView and implemented the paint method as follows:

    void MyTreeView::drawRow (QPainter* painter,
                              const QStyleOptionViewItem& option,
                              const QModelIndex& index) const
    {
      QStyleOptionViewItem newOption(option);

      if (someCondition)
      {
        newOption.palette.setColor( QPalette::Base, QColor(255, 0, 0) );
        newOption.palette.setColor( QPalette::AlternateBase, QColor(200, 0, 0) );
      }
      else
      {
        newOption.palette.setColor( QPalette::Base, QColor(0, 0, 255) );
        newOption.palette.setColor( QPalette::AlternateBase, QColor(0, 0, 200) );
      }

      QTreeView::drawRow(painter, newOption, index);
    }

Initially, I set setAlternatingRowColors(true); for the QTreeView.

My problem: Setting the color for QPalette::Base has no effect. Every second row remains white.

However, setting QPalette::AlternateBase works as expected. I tried setAutoFillBackground(true) and setAutoFillBackground(false) without any effect.

Are there any hints how to solve this problem? Thank you.


Remark: Setting the color by adapting MyModel::data(const QModelIndex&, int role) for Qt::BackgroundRole does not provide the desired result. In this case, the background color is used only for a part of the row. But I want to color the full row, including the left side with the tree navigation stuff.

Qt Version: 4.7.3


Update: For unknown reasons QPalette::Base seems to be opaque. setBrush does not change that. I found the following workaround:

    if (someCondition)
    {
        painter->fillRect(option.rect, Qt::red);
        newOption.palette.setBrush( QPalette::AlternateBase, Qt::green);
    }
    else
    {
        painter->fillRect(option.rect, Qt::orange);
        newOption.palette.setBrush( QPalette::AlternateBase, Qt:blue);
    }

解决方案

If the only problem is that the expanding/collapsing controls do not have a background like rest of the row then use Qt::BackgroundRole in ::data() of your model (as described by pnezis in their answer) and add this to your tree view class:

void MyTreeView::drawBranches(QPainter* painter,
                              const QRect& rect,
                              const QModelIndex& index) const
{
  if (some condition depending on index)
    painter->fillRect(rect, Qt::red);
  else
    painter->fillRect(rect, Qt::green);

  QTreeView::drawBranches(painter, rect, index);
}

I've tested this on Windows (Vista and 7) using Qt 4.8.0 and expanding/collapsing arrows have proper background. The problem is that those arrows are part of the view and thus cannot be handled in a model.

这篇关于更改QTreeView的行背景颜色不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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