Как добавить иконку в QTreeView?
Как добавить иконку в QTreeView?
К примеру иконку перед "main.spct". По типу такого:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
tree = \
{
'Project':
{
'src':
{
'main.spct': {}
}
}
}
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.tree_view = QtWidgets.QTreeView()
self.setCentralWidget(self.tree_view)
model = QtGui.QStandardItemModel()
self.populateTree(tree, model.invisibleRootItem())
self.tree_view.setModel(model)
model.setHeaderData(0, QtCore.Qt.Horizontal, 'File Manager')
self.tree_view.expandAll()
self.tree_view.selectionModel().selectionChanged.connect(self.onSelectionChanged)
def populateTree(self, children, parent):
for child in children:
child_item = QtGui.QStandardItem(child)
parent.appendRow(child_item)
if isinstance(children, dict):
self.populateTree(children[child], child_item)
def onSelectionChanged(self, *args):
for sel in self.tree_view.selectedIndexes():
val = sel.data()
while sel.parent().isValid():
sel = sel.parent()
val = sel.data()+ val
print(val)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Некоторые варианты выглядят так:
from PyQt5 import QtCore, QtGui, QtWidgets
tree = \
{
'Project':
{
'src':
{
'main.spct': {}
}
},
"No Category":
{
'New Game':
{
'Playnite': {'Never'}
}
}
}
class GroupDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
super(GroupDelegate, self).__init__(parent)
self._plus_icon = QtGui.QIcon('Ok.png') # установите свои изображения
self._minus_icon = QtGui.QIcon('ball.png') # ...
def initStyleOption(self, option, index):
super(GroupDelegate, self).initStyleOption(option, index)
if not index.parent().isValid():
is_open = bool(option.state & QtWidgets.QStyle.State_Open)
option.features |= QtWidgets.QStyleOptionViewItem.HasDecoration
option.icon = self._minus_icon if is_open else self._plus_icon
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.tree_view = QtWidgets.QTreeView()
self.setCentralWidget(self.tree_view)
# +++
pixmap = QtGui.QPixmap(128, 128)
pixmap.fill(QtCore.Qt.green)
self.icon = QtGui.QIcon(pixmap)
model = QtGui.QStandardItemModel() # +++
self.populateTree(tree, model.invisibleRootItem())
delegate = GroupDelegate(self)
self.tree_view.setItemDelegateForColumn(0, delegate)
self.tree_view.setModel(model)
model.setHeaderData(0, QtCore.Qt.Horizontal, 'File Manager')
self.tree_view.expandAll()
self.tree_view.selectionModel().selectionChanged.connect(
self.onSelectionChanged)
def populateTree(self, children, parent):
for child in children:
child_item = QtGui.QStandardItem(child)
child_item.setIcon(self.icon) # +++
if child == "main.spct":
icon = QtGui.QIcon('icons/qtlogo.png') # ...
icon.pixmap(155, 55)
child_item.setIcon(icon)
parent.appendRow(child_item)
if isinstance(children, dict):
self.populateTree(children[child], child_item)
def onSelectionChanged(self, *args):
for sel in self.tree_view.selectedIndexes():
val = sel.data()
while sel.parent().isValid():
sel = sel.parent()
val = sel.data() + '/' + val
print(f'val = {val}\n')
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())