QTreeWidget交替行颜色设置
我想将颜色设置为我使用的树小部件中的备用行
setAlternatingRowColors(1);
QPalette p = palette();
p.setColor( QPalette::AlternateBase, QColor(226, 237, 253) );
setPalette(p);
但是每次单击后,颜色设置为已设置行下方的行,或者颜色设置在行之间切换。我希望将其设置为特定行的常量。意味着首先如果第二行正在设置颜色,那么单击后颜色设置将转到第三行。我希望它只位于第二行
I want to set color to alternate row in the treewidget I did with
setAlternatingRowColors(1);
QPalette p = palette();
p.setColor( QPalette::AlternateBase, QColor(226, 237, 253) );
setPalette(p);
But here after every single click the color is setting to the row below the already set row or the color setting is toggling between rows. I want it to be set constant to particular row. Means first if 2nd row is setting color then after single click the color set is going to 3rd row. I want it to be in 2nd row only
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用模型来执行此操作,并为模型中的背景返回适当的颜色。当
data(const QModelIndex& index, int role)
被称为视图的模型对象(或在您的情况下为QTreeWidget
)时,role 的值之一
将是Qt::BackgroundRole
。像下面这样的东西会做你想要的:I suggest using the model to do this and return the approrpriate color for the background in your model. When
data(const QModelIndex& index, int role)
is called the model object for the view (orQTreeWidget
in your case), one of the values ofrole
will beQt::BackgroundRole
. Something like the following would do what you want: