7

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;
}
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97

2 Answers2

11

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;
}
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
-1

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));
  • Unfortunately, setting the palette color doesn't remove the blue artifact. Palettes aren't widely respected by Qt styles and usually don't play nicely with style sheets. – Nicolas Holthaus Mar 23 '15 at 11:25
  • @NicolasHolthaus Except, I'm currently using a stylesheet and its the ONLY way I was able to successfully get rid of the blue highlight in all widgets. The solution I have given is also used by some stylesheet repos on GitHub. I took it from the QDarkStyle repo to be exact. – Matt Guerrette Mar 24 '15 at 13:01
  • All I can say is it didn't fix the problem in my application, but the 'selection-background-color' property did. Perhaps we're using different Qt versions (I'm using 5.4.2), or maybe the root cause of our problem wasn't really the same. – Nicolas Holthaus Mar 24 '15 at 13:29
  • It looks like `QDarkStyle` is written in python. I'm using c++ Qt, so there might be a discrepancy there as well. – Nicolas Holthaus Mar 24 '15 at 13:35
  • I'm using C++ with Qt as well. My specific issue was with the QMainWindow menubar submenus. None of the QSS would be applied to those. I will attempt it again later today. – Matt Guerrette Mar 25 '15 at 17:23