首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改QTreeView的行背景色不起作用

问更改QTreeView的行背景色不起作用
EN

Stack Overflow用户
提问于 2013-01-10 10:04:19
回答 3查看 13.7K关注 0票数 11

我有一个QTreeView,并想要不同的背景色行,根据其内容。为了实现这一点,我从class MyTreeView派生了一个QTreeView,并按如下方式实现了paint方法:

代码语言:javascript
复制
    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(true);设置为QTreeView。

My problem:为QPalette::Base设置颜色没有影响。第二排保持白色。

但是,设置QPalette::AlternateBase与预期的一样工作。我尝试了setAutoFillBackground(true)和setAutoFillBackground(false),但没有任何效果。

有什么提示可以解决这个问题吗?谢谢。

注意:通过为MyModel::data(const QModelIndex&, int role)设置Qt::BackgroundRole颜色并不能提供所需的结果。在这种情况下,背景色仅用于行的一部分。但是我想给整行涂上颜色,包括左边和树导航的东西。

Qt版本: 4.7.3

更新:由于未知的原因QPalette::Base似乎是不透明的。setBrush并没有改变这一点。我找到了以下解决办法:

代码语言:javascript
复制
    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);
    }
EN

回答 3

Stack Overflow用户

发布于 2013-01-13 02:05:58

如果唯一的问题是展开/折叠控件没有类似行其余部分的背景,那么在模型的Qt::BackgroundRole中使用::data() (如pnezis在their answer中描述的那样),并将其添加到树视图类中:

代码语言:javascript
复制
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);
}

我已经使用QT4.8.0在Windows (Vista和7)上测试了这一点,展开/折叠箭头有合适的背景。问题是,这些箭头是视图的一部分,因此不能在模型中处理。

票数 10
EN

Stack Overflow用户

发布于 2013-01-10 11:15:33

您应该通过模型处理背景色,而不是对QTreeView进行子类化。使用data()函数和Qt::BackgroundRole更改行的背景色。

代码语言:javascript
复制
QVariant MyModel::data(const QModelIndex &index, int role) const
{
   if (!index.isValid())
      return QVariant();

   if (role == Qt::BackgroundRole)
   {
       if (condition1)
          return QColor(Qt::red);
       else
          return QColor(Qt::green); 
   }

   // Handle other roles

   return QVariant();
}
票数 8
EN

Stack Overflow用户

发布于 2017-03-17 04:46:10

https://www.linux.org.ru/forum/development/4702439

代码语言:javascript
复制
if ( const QStyleOptionViewItemV4* opt = qstyleoption_cast<const QStyleOptionViewItemV4*>(&option) )
{
        if (opt.features & QStyleOptionViewItemV4::Alternate)
            painter->fillRect(option.rect,option.palette.alternateBase());
        else
            painter->fillRect(option.rect,painter->background());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14255224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档