Probleme with Grid Lines and QTreeView
-
Hi,
I am using a QTreeView with a custom delegate and a subclassed QAbstractModel.
I would like to see gridlines for items without children.At first display , everything's looks fine but whenever the mouse hovers over an item, the bottom or top line dissappears and reappears when hovering back to the item.
First display is ok :
!http://img4.hostingpics.net/pics/836040initialView.png(Initial View)!after hovering mouse over the last child ( commentaires )
!http://img4.hostingpics.net/pics/339620hoveringView.png(hovering view)!I am using the following stylesheet for the QTreeItemView :
@ QString treeViewStyle =
"QTreeView { show-decoration-selected: 1; }"
"QTreeView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 white);}"
"QTreeView::item:selected:active { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 lightgray); color: black;}"
"QTreeView::item:selected:!active { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 lightgray); color: black;}" ;
ui->ProjetTreeView->setStyleSheet(treeViewStyle);@The paint method is reimplemented like this in my custom delegate :
@if (index.column() > 0 && !index.model()->hasChildren(index))
{
QPen pen;
pen.setColor(QColor(Qt::lightGray));
pen.setWidth(1);
painter->setPen(pen);
painter->drawRect(option.rect);
QStyledItemDelegate::paint(painter,option,index);
}
else QStyledItemDelegate::paint(painter,option,index);@Has anyone an idea of how to get rid of this dynamic behavior and keep the initial correct view at first display ?
Hope this description is quiet clear, if not feel free to ask me more details, this is my first post here ;)
Thanks a lot,
-
welcome to forum. Can you try removing the following line in stylesheet and see how it works.
QTreeView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 white);}"
-
Thanks for this answer.
I have removed the line and even without using stylesheet at all, the problem is still here.
I understand that, each time an item is hovered, the view is calling the paint method from the delegate and passing all its indexes of the item. Say my item is at row 2 and i have 3 columns, then the corresponding index.row() and index.column() in the paint method will show (2,0) ; (2;1) ; (2;2).
Just to complete my previous post, i don't subclass QTreeView.
-
I finally found a way to get a similar display without this mouse hovering issue, using stylsheet.
My paint method is simply calling the base class paint method from QStyledItemDelegate
@void ProjetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter,option,index);
}@and the stylesheet is defined as :
@QString treeViewStyle =
"QTreeView { show-decoration-selected: 1; }"
"QTreeView::item:has-children:open {background-color: lightgray;}"
"QTreeView::item:!has-children { border: 0.5px ; border-style: solid ; border-color: lightgray ;}"
"QTreeView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 white); }"
"QTreeView::item:selected:active { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 lightgray); color: black; }"
"QTreeView::item:selected:!active { background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 lightgray, stop: 1 lightgray); color: black;}" ;
ui->ProjetTreeView->setStyleSheet(treeViewStyle);@leading to the following result :
!http://img4.hostingpics.net/pics/586918Alternativesolution.png(alternative solution)!However, it's not exactly what i wanted intially and i hope there is a better solution.
Thanks for your help.