如何更改(删除)QListWidget 的选择/活动颜色 [英] How to change (remove) selection/active color of QListWidget

查看:147
本文介绍了如何更改(删除)QListWidget 的选择/活动颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 QListWidget 中,有一些项目具有非默认背景色,我在自定义 QListWidget 类中像这样设置它们:

In my QListWidget, there are some items that have non-default background color, I set them like so inside the custom QListWidget class:

item->setBackgroundColor(qcolor); // item is of type QListWidgetItem*

我设置的那些非默认颜色被 QListWidget 的选择颜色扭曲了.看一个例子:

Those non-default colors that I set are distorted by the QListWidget's selection color. See an example:

项目 threefour 应该是相同的颜色,但它们不是,因为项目 four 被选中,因此结果颜色是原始颜色和 QListWidget 的选择(活动项目?)颜色的总和.

Items three and four are supposed to be the same color, but they are not since the item four is selected, and thus the result color is a summation of original color and QListWidget's selection (active item?) color.

我的问题是如何编辑或删除该选择颜色?

My question is how to edit or remove that selection color?

我在我的 QListWidget 中尝试过(当我想更改项目的背景颜色时,在特殊插槽中):

I tried inside my QListWidget (in special slot when I want to change the item's background color):

QPalette pal = this->palette();
pal.setColor(QPalette::Highlight, QColor(255,255,255,0));
this->setPalette(pal); // updated

但是没有产生任何效果.我究竟做错了什么?设置正确的变量吗?我是在 QListWidget 中还是在它的委托中设置它?

But it did not produce any effect. what am I doing wrong? Is it a correct variable to set? Do I set it up inside QListWidget or inside its delegate?

更新:我尝试使用评论/答案指出的样式表,但是,无法将它们用于我的应用程序,因为我行中的项目有 3 个状态(所以我会需要使用三种颜色).例如,对应于三种颜色的 3 个状态:活跃的粉红色,不活跃的绿色和其余的灰色.使用样式表时,我无法将自定义属性(比方说 QListWidget::item[Inactive="true"])设置为单个 QListWidgetItem,但对于完整的 QListWidget,因此它为所有行着色相同的颜色.

Update: I tried using stylesheets as pointed by comment/answer, however, it will not be possible to use them for my application, because the items in my rows have 3 states (so I would need to use three colors). E.g., 3 states that correspond to three colors: pink for active, green for inactive and gray for the rest. When using stylesheets, I cannot set the custom property (let's say QListWidget::item[Inactive="true"]) to a single QListWidgetItem, but for the full QListWidget, and therefore it colors all the rows the same color.

针对类似问题尝试了样式表此处,并没有工作,因此我得出结论使用样式表不是要走的路.

Stylesheets were tried for similar problem here, and didn't work, therefore I make conclusion using stylesheets is not the way to go.

我最初使用的背景更改方法可以很好地满足我的目的,但我无法弄清楚如何摆脱添加到背景颜色并产生混合颜色的默认选择颜色(透明浅蓝色).

The background change method that I used originally works fine for my purpose, but I cannot figure out how to get rid of the default selection color (transparent light blue) which adds to the background color and produces the mixed color.

推荐答案

我通过使用委托找到了合适的解决方案.所以,没有必要使用QPalette;对于我的问题,样式表将不起作用.当需要根据某些状态将不同的行(QListWidgetQTreeWidget)着色为不同的颜色时,此解决方案也适用.

I found a suitable solution by using a delegate. So, there is no need to use QPalette; and for my problem the stylesheets will not work. This solution will also work when different rows (QListWidget or QTreeWidget) are needed to be colored in different colors, depending on some state.

背景颜色设置如问题所述:

The background color is set as described on the question:

item->setBackgroundColor(qcolor); // change color slot inside QListWidget class

为了定义小部件的绘制规则,我重新定义了委托:

In order to define rules how the widget is painted, I re-define the delegate:

class Delegate : public QStyledItemDelegate {};

然后我重新定义了Delegate的方法paint().在那里我定义了如何绘制小部件的每一行.更具体地说,我只在鼠标悬停在项目上时调用自定义绘图,或者该项目处于选中状态(这些是我想避免的浅蓝色选择行时的条件).代码如下所示:

Then I re-define Delegate's method paint(). There I define how to draw each row of my widget. More specifically, I only call custom drawing when the mouse hovering over an item, or that item is in selected state (those are the conditions when the row is selected by the light blue color which I want to avoid). The code looks like this:

void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if((option.state & QStyle::State_Selected) || (option.state & QStyle::State_MouseOver))
    {
        // get the color to paint with
        QVariant var = index.model()->data(index, Qt::BackgroundRole);

        // draw the row and its content
        painter->fillRect(option.rect, var.value<QColor>());
        painter->drawText(option.rect, index.model()->data(index, Qt::DisplayRole).toString());
    }
    else
        QStyledItemDelegate::paint(painter, option, index);

    // ... 
}

当然不要忘记将QListWidgetDelegate关联起来:

Of course, do not forget to associate the QListWidget with Delegate:

listWidget->setItemDelegate(new Delegate());

这篇关于如何更改(删除)QListWidget 的选择/活动颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆