Issue
Is there any way to find if an item in a tree view is in which state in PyQt5? ie, expanded or collapsed. I need to get the value in the custom delegate class that I have created.
class SummaryDelegate(QStyledItemDelegate):
def __init__(self, treeView):
super(SummaryDelegate, self).__init__()
self.treeView = treeView
self.headerItems = self.collectDictKeys(summarySectionData)
def collectDictKeys(self, data):
collection = []
for key, value in data.items():
collection.append(key)
if isinstance(value, dict):
collection.extend(self.collectDictKeys(value))
return collection
def paint(self, painter, option, index):
dataItem = index.data()
if dataItem in self.headerItems:
pass
else:
if type(index.data()) == str:
pass
else:
pass
newRec = QRect(option.rect)
newRec.setLeft(0)
painter.fillRect(newRec, QColor(240, 245, 255))
Solution
In paint
, option.state
will give you the state of the item including whether it's open (i.e. expanded) or not. It also has information about other state parameters of the item such as whether it's selected or not. For find out if the item is open or not you can use something like int(option.state) & QtWidgets.QStyle.State_Open
. Similarly, int(option.state) & QtWidgets.QStyle.State_Selected
will tell you whether the item is selected or not.
Answered By - Heike
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.