PDA

View Full Version : QTreeView show-decoration-selected has no effect



stefanadelbert
23rd June 2010, 00:51
I'm trying to change the selection behaviour in a QTreeView using a stylesheet. The show-decoration-selected (http://doc.qt.nokia.com/4.6.2/stylesheet-reference.html#show-decoration-selected-prop) property seems to have no effect.

I'm calling the following in my QMainWindow:


setStyleSheet(
"QTreeView {"
" selection-background-color: navy;"
" alternate-color: white;"
" alternate-background-color: rgb(240,240,240);"
" show-decoration-selected: 1;"
"}"
);

The alternate-background-color property does have an effect, so I know that the QTreeView is using the stylesheet. But switching show-decoration-selected from 0 to 1 has no effect at all. How should the show-decoration-selected property affect a QTreeView?

I would like to have the entire row selected from the extreme left of the QTreeView to the extreme right of the QTreeView regardless of the level/depth of the item in the QTreeView. And I would like the selection-background-color to be navy and selection-color to be white.

I have had this styling working for a QTableView. Any reason why it shouldn't be possible for a QTreeView?

I'm on Windows 7 using C++ Qt 4.6.2 in Visual Studio 2008.

stefanadelbert
23rd June 2010, 01:25
This is how I create and initialise the QTreeView in my CTreeViewWidget.



treeView.reset(new QTreeView(this));
treeView->setModel(&model);
treeView->setFocusPolicy(Qt::NoFocus);
treeView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
treeView->setAlternatingRowColors(true);
treeView->setSelectionMode(QAbstractItemView::ExtendedSelect ion);
treeView->setSelectionBehavior(QAbstractItemView::SelectRows );
treeView->setVerticalScrollMode(QAbstractItemView::ScrollPer Pixel);
treeView->setHorizontalScrollMode(QAbstractItemView::ScrollP erPixel);
treeView->setSortingEnabled(false);
treeView->header()->setStretchLastSection(false);
treeView->header()->setDefaultAlignment(Qt::AlignCenter);
treeView->header()->setDefaultAlignment(Qt::AlignHCenter);
treeView->header()->setHighlightSections(false);
treeView->header()->setMovable(true);


I've also noticed that the last section of the header stretches to the end even though I call setStretchLastSection(false). I would prefer this not to happen. Any ideas on how to prevent the last section from stretching?

stefanadelbert
29th June 2010, 01:38
bump, bump, bump, bump.

stefanadelbert
1st July 2010, 23:42
Right, it seems that "show-decoration-selected" does have an effect, just not the effect that I was expecting. On Windows 7 it "glows" the expand/collapse icon to the left of a node with children.

However the description of the property does not explain this behaviour at all:


Controls whether selections in a QListView cover the entire row or just the extent of the text.

If this property is not specified, it defaults to the value specified by the current style for the SH_ItemView_ShowDecorationSelected style hint.

Example:

* { show-decoration-selected: 1 }

I have managed to achieve something closer to what I'm after by using a stylesheet, particularly:


QTreeView, QTableView{
show-decoration-selected: 1;
selection-color: white;
alternate-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #EFEFEF, stop: 1 #DFDFDF);
}

QTreeView::item {
border: transparent;
}

QTreeView::item:!selected:hover {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #D0D0D0, stop: 1 #CFCFCF);
}

QTreeView::item:selected:!hover{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #8F9CBF, stop: 1 #838FAF);
}

QTreeView::item:selected:hover{
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #7888AF, stop: 1 #6D7C9F);
}