QTreeWidget备用行颜色设置
我想将颜色设置为我使用的树边中的备用行QTreeWidget备用行颜色设置,qt,qt4,Qt,Qt4,我想将颜色设置为我使用的树边中的备用行 setAlternatingRowColors(1); QPalette p = palette(); p.setColor( QPalette::AlternateBase, QColor(226, 237, 253) ); setPalette(p); 但在这里,每次单击后,颜色设置为已设置行下方的行,或者颜色设置在行之间切换。我希望将其设置为特定行的常量。表示第一行,如果第二行正在设置颜色,则单击后,颜色集将转到第三行。我希望它只在第二排我建议使用
setAlternatingRowColors(1);
QPalette p = palette();
p.setColor( QPalette::AlternateBase, QColor(226, 237, 253) );
setPalette(p);
但在这里,每次单击后,颜色设置为已设置行下方的行,或者颜色设置在行之间切换。我希望将其设置为特定行的常量。表示第一行,如果第二行正在设置颜色,则单击后,颜色集将转到第三行。我希望它只在第二排我建议使用模型来完成这项工作,并返回模型中背景的适当颜色。当
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;
}
}