[Python/PyQt] How to make arrow in QTreeView::branch visible after adjusting selection background
-
hi everyone,
How can I make the arrow in QTreeView::branch visible after adjusting selection-background-color in stylesheet?
Here is my code:
@ #!/usr/bin/env pythonfrom PyQt4.QtCore import * from PyQt4.QtGui import * class MainWindow(QMainWindow): def __init__(self, parent=None, **kwargs): QMainWindow.__init__(self, parent=None, **kwargs) self.setStyleSheet(""" QTreeView::item { background-color: rgb(49,49,49); border-left: 1px solid rgb(40,40,40); } QTreeView::item::selected { background-color: rgb(103,95,92); color: rgb(40,40,40); } QTreeView QHeaderView:section { background-color: rgb(44,44,44); color: rgb(133,133,133); } QTreeView::branch { selection-background-color: blue; } """) self.tree = QTreeWidget(self) self.tree.resize(300,300) self.parent = QTreeWidgetItem(self.tree, "test") self.child = QTreeWidgetItem(self.parent, ["test", "test", "test"]) if __name__=="__main__": from sys import argv, exit a=QApplication(argv) m=MainWindow() m.resize(300,300) m.show() m.raise_() exit(a.exec_())
@
Thanks!! :)
-
Any ideas? :(
-
I'm not sure I understand the issue. When I run your code I see the arrow for the parent item whether selected or not. The child won't have an arrow because it has no children itself, though it will be indented so that space is available should it be required in the future. Are you getting a different result of have I misunderstood the query?
-
Jazzy,
Here is what I mean,
Before clicking a QTreeWidgetItem in a QTreeWidget:
!http://oi42.tinypic.com/23vgtnl.jpg(beforeClick)!
Clicking a QTreeWidgetItem: in a QTreeWidget
!http://oi40.tinypic.com/15noqow.jpg(clickingItem)!
I hope this is more clear.
Thanks!!
-
When using stylesheets and overriding the branch style you have to explicitly specify the (user supplied) images you would like to use for the drop indicators (as well as those for the interconnecting lines). The last two sections of the stylesheet in the following example demonstrate this:
@
from PyQt4.QtCore import *
from PyQt4.QtGui import *class MainWindow(QMainWindow):
def init(self, parent=None, **kwargs):
QMainWindow.init(self, parent=None, **kwargs)self.setStyleSheet(""" QTreeView::item { background-color: rgb(49,49,49); border-left: 1px solid rgb(40,40,40); } QTreeView::item::selected { background-color: rgb(103,95,92); color: rgb(40,40,40); } QTreeView QHeaderView:section { background-color: rgb(44,44,44); color: rgb(133,133,133); } QTreeView::branch { background: rgb(49,49,49); } QTreeView::branch::closed::has-children { image: url(branch-closed.png); } QTreeView::branch::open::has-children { image: url(branch-open.png); } """) self.tree = QTreeWidget(self) self.tree.resize(300,300) self.parent = QTreeWidgetItem(self.tree, "test") self.child = QTreeWidgetItem(self.parent, ["test", "test", "test"])
if name=="main":
from sys import argv, exita=QApplication(argv) m=MainWindow() m.resize(300,300) m.show() m.raise_() exit(a.exec_())
@
I borrowed the images from the documentation "here":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-qtreeview which also details all the selectors for doing this.
Hope this helps ;o)
-
I was looking for a solution without having to involve images.
But I'll use this for my last resort. Thanks so much!!