Nicolas Holthaus
Nicolas Holthaus

Reputation: 8293

Remove blue selection from QTreeView in "fusion" style

I have a QTreeView with a stylesheet defining the selection. However, when I use the "fusion" style, there is an additional blue selection rectangle over the decoration:

blue selection rectangle

I've tried using show-decoration-selected: 0; in the style sheet, as well as setting setAllColumnsShowFocus(false);, but can't get it to go away.

Is there some way to disable or restyle the part of the selection that covers the decorator?

For reference, here's the stylesheet:

QTreeView, QListView, QToolBar, QTableView
{
    show-decoration-selected: 0;
    background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,
                            stop: 0 #797979, stop: 0.2 #CCCCCC,
                            stop: 1.0 #FFFFFF);
    alternate-background-color: #333333;
    background-image: url(:/metal_scratched);
    outline: 0; /* removes focus rectangle*/
}

QTreeView::section, QListView::section, QToolBar::section, QTableView::section
{
    border: 1px solid black;
}

QTreeView::item:hover:enabled, QListView::item:hover:enabled, QToolBar::item:hover:enabled, QTableView::item:hover:enabled
{
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                            stop: 0 transparent, stop: 0.4 rgba(150,150,150,0.5),
                            stop: 0.5 rgba(125,125,125,0.5), stop: 1.0 transparent);
    border-color: #B0B0B0;
}

QTreeView::item:hover:!enabled, QListView:disabled:hover, QToolBar:disabled:hover, QTableView:disabled:hover
{
 /* don't highlight */
}

QTreeView::item:selected, QListView::item:selected, QToolBar::item:selected, QTableView::item:selected
{
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                            stop: 0 transparent, stop: 0.4 rgba(75,75,75,0.5),
                            stop: 0.5 rgba(50,50,50,0.5), stop: 1.0 transparent);
    border-color: #5A5A5A;
    color: white;
}

Upvotes: 7

Views: 4319

Answers (2)

Nicolas Holthaus
Nicolas Holthaus

Reputation: 8293

The blue artifact can be removed by overriding the default selection color in the style sheet. Leaving everything else the same (importantly, continuing to define a new selection color using QTreeView::item:selected), adding the following property will remove the undesired behavior.

QTreeView
{
    // everything else the same
    selection-background-color: transparent;
}

Upvotes: 11

Matt Guerrette
Matt Guerrette

Reputation: 24

Under further review, I found that the only way to truly accomplish this is to set the palette of you Qt Application.

Like so,

QApplication app(argc, argv);
app.setStyle(QStyleFactory::create("fusion"));
QPalette palette;
palette.setColor(QPalette::Highlight, QColor(your-color));

Upvotes: -1

Related Questions