在Qt 5中更改QTreeView文本颜色

时间:2015-04-23 13:51:42

标签: c++ qt qt5 qtstylesheets

我将QTreeView用于我的模型。 CSS描述的QTreeView样式(灰色背景和白色文本)。我在树上实现了搜索,并为找到的行应用了另一种颜色(黄色背景和黑色文本)。为此我重新实现drawRow()函数:

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

  if (m_findedIndexes.contains(index))
  {
    newOption.palette.setColor(QPalette::Text, QColor(0, 0, 0));
    painter->fillRect(newOption.rect, Qt::yellow);
  }
  QTreeView::drawRow(painter, newOption, index);
}

在Qt 4.7.1中,一切正常。我转向Qt 5.4.0并遇到了逆境。背景颜色未更改(黄色)但文本颜色保持白色(在CSS中)。文字的黑色不适用。我不明白为什么会这样。可能在QT 5.4.0中CSS样式的优先级高于QStyleOptionViewItem吗?我该如何更改文字颜色?

P.S。我试图使用QStyledItemDelegate,我试图从我的模型返回文本颜色(标志Qt :: ForegroundRole)。没有结果。

0 个答案:

没有答案