How to change (remove) selection/active color of QListWidget

时间秒杀一切 提交于 2019-12-10 11:33:32

问题


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*

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

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?

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

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?

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.


回答1:


I think you'd be better served using the style sheets to do this. Here's an example

QListWidget::item
{
    background: rgb(255,255,255); 
}
QListWidget::item:selected
{
    background: rgb(128,128,255);
}

::item indicates the individual items within the QListWidget, while :selected indicates the QListWidgetItems which are currently selected.

To then get the custom background on specific widgets, you could use custom style sheet properties. In your code, call something like this on the widget you want to apply a custom style on:

myList->setProperty( "Custom", "true" );

//  Updates the style.
style->unpolish( myList );
style->polish( myList );

Then in your style sheet, define the style for your custom property like so.

QListWidget::item[Custom="true"]
{
    background: rgb(128,255,128);
}



回答2:


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 {};

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);

    // ... 
}

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

listWidget->setItemDelegate(new Delegate());


来源:https://stackoverflow.com/questions/36018010/how-to-change-remove-selection-active-color-of-qlistwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!