我有一個QTreeView
,並且需要不同的行背景顏色,具體取決於它們的內容。爲了實現這一點,我衍生自QTreeView
一個class MyTreeView
和如下實施的塗料方法:更改QTreeView的行背景顏色不起作用
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則。
我的問題:設置顏色爲QPalette :: Base沒有影響。每隔一行保持白色。
但是,設置QPalette :: AlternateBase按預期工作。 我試過setAutoFillBackground(true)
和setAutoFillBackground(false)
沒有任何影響。
有沒有什麼提示如何解決這個問題?謝謝。
注:通過調整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);
}
我想這一點,但是這並不能產生預期的結果。採用這種方法,背景顏色不會塗滿整行,而只能塗抹樹項目本身。樹導航的背景(在項目的左側)沒有改變。 – SebastianK
@SebastianK我認爲這仍然是正確的方法,但是在這個例子中實現'condition1'的方式是關鍵。例如,你可以做'if(index.sibling(index.row(),someColumnIndex).data()== whatever){...}'。當然,你必須確保這個函數沒有被調用到'index.column()== someColumnIndex'的情況。 –
@TimMeyer我試着用'condition1'來保證總是真的。該行的背景僅部分呈紅色。對於擴展項目,行的左側部分仍然是白色。對於「左邊部分」,我指的是具有樹形導航內容的區域(擴展/摺疊[+]/[ - ]和父行) – SebastianK