2011-08-09 76 views
2

我想設置顏色交替行中的treewidget我QTreeWidget交替行顏色設置

setAlternatingRowColors(1); 
QPalette p = palette(); 
p.setColor(QPalette::AlternateBase, QColor(226, 237, 253)); 
setPalette(p); 

但是這裏經過的每一個點擊的顏色確實是已經設定的行或顏色設置低於該行設置在行之間切換。我希望它被設置爲特定行不變。首先如果第二排設置顏色,然後單擊顏色設置到第三排。我希望它只在第二行

回答

1

我建議使用模型來做到這一點,並返回模型中背景的approrpriate顏色。當data(const QModelIndex& index, int role)被稱爲視圖的模型對象(或在您的情況下爲QTreeWidget)時,role的其中一個值將爲Qt::BackgroundRole。像下面這樣會做你想做的事:

QVariant SomeModel::data(const QModelIndex& index, int role) 
{ 
    switch(role) 
    { 
    // other role handling code here. below is the except for handling BackgroundRole 
    case Qt::BackgroundRole: 
     if (0 == index.row() % 2) 
      return QColor(226, 237, 253); 
     else 
      return Qt::white; 
    break; 
    } 
} 
+0

這不像你在樹視圖中所期望的那樣工作。 –